WPF 바인딩 질문있습니다.

txt_input, txt_output 두가지 텍스트 박스가 있습니다.
txt_input에 1000이란 값을 넣으면
바인딩된 num에 1000 값이 할당되게됩니다.

그리고 또다른 num을 바인딩하여 값을 출력하는 txt_output에
500이란 입력된 숫자값의 절반이 출력되는 방법이 있는지 궁금합니다.

하고 싶은것

  • 세금계산에서 부가세 계산 등에 사용할 예정입니다. (가격 입력시 옆 텍스트박스에 0.1자동 계산)
  • 엑셀 수식처럼 가능한지 궁금합니다.

혹시 방법이 있나요?

1개의 좋아요

아웃풋텍스트박스.텍스트={Binding num, Converter=(입력값을 적절히 변환하는 컨버터)}

컨버터를 사용하시거나

CalcBinding 을 사용해보세요.

1개의 좋아요

@favdra 님의 의견에 추가 답변입니다.

 <TextBox x:name="input"/>
 <TextBox Text={Binding Path=Text, Element=input, Converter=(입력값을 적절히 변환하는 컨버터)}
1개의 좋아요

참조) NuGet\Install-Package CommunityToolkit.Mvvm -Version 8.2.2

가) ViewModel.cs

public class ViewModel : ObservableObject
    {
        public ViewModel()
        {
        }

        public double Price
        {
            get => _price;
            set
            {
                SetProperty(ref _price, value);
                Tax = value * TaxRate;
            }
        }
        private double _price;

        public double Tax
        {
            get => _tax;
            set => SetProperty(ref _tax, value);
        }
        private double _tax;

        private static readonly double TaxRate = 0.1D;
    }

나) View.xaml

<UserControl x:Class="Lichen.NetDev.Wpf.View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Lichen.NetDev.Wpf"
             mc:Ignorable="d" 
             d:DataContext="{d:DesignInstance local:ViewModel, IsDesignTimeCreatable=True}"
             d:DesignHeight="50" d:DesignWidth="100">
    <UniformGrid Rows="2">
        <TextBox Text="{Binding Path=Price}" TextAlignment="Right" VerticalContentAlignment="Center"/>
        <TextBox Text="{Binding Path=Tax}" IsReadOnly="True" TextAlignment="Right" VerticalContentAlignment="Center"/>
    </UniformGrid>
</UserControl>

다) View.xaml.cs

public partial class View : UserControl
    {
        public View()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }

라) MainWindow.xaml

<Window x:Class="Lichen.NetDev.Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Lichen.NetDev.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Viewbox>
        <local:View/>
    </Viewbox>
</Window>
3개의 좋아요

모두 답변 감사합니다.

새로 알려주신 calcBinding은 한번 확인해보고 나중에 한번 사용해봐야겠네요.

감사합니다.!!

1개의 좋아요