1
votes

My application is WPF and I've faced up with the issue below, please show me direction in order to overcome it. Let's assume I have 3 textboxes:

<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />

All the textboxes will display numeric strings. The textBox1.Text is assigned in code behind, textBox2.Text is assigned by user input and what I want to is to assign textBox3.Text on textChanged of textBox2 this way:

textBox3.text = string.Format("{0:0.00}", double.Parse(textBox2.Text) - double.Parse(textBox1.Text));

Please, tell me how to achieve this?

2
delete all that and start reading here.Federico Berasategui
What are the issues you are facing?Gun
@HighCore, thank you for the link, just reading it.khurshed_nosirov
@Gun, the problem is that I want to bind textBox3.Text not to exactly what is typed in textBox2, but I want the input effect the target textBox3.Text. As far as I've been through the link above what I need is Data Conversion while Binding. Am I right? If so, how could I implement it in my case?khurshed_nosirov

2 Answers

1
votes

It can be achieved easily if you use MVVM pattern.

ViewModel

public string TextBox1Text
{
    get{return _text1;}
    set{_text1 = value; NotifyPropertyChanged("ComputedProperty")}
}

public string TextBox2Text
{
    get{return _text2;}
    set{_text2 = value; NotifyPropertyChanged("ComputedProperty")}
}

public string ComputedProperty
{
    get {return string.Format("{0:0.00}", double.Parse(TextBox2Text) - double.Parse(TextBox1Text));}
}

XAML

<TextBox Text="{Binding TextBox2Text, Mode=TwoWay}" />
<TextBox Text="{Binding TextBox1Text, Mode=TwoWay}" />    
<TextBox Text="{Binding ComputedProperty}" />
1
votes

Thanks to the link above from HighCore I was finally able to make it. In my case textBox3.Text depends on the other two textBoxes, so what I need here is MultiBinding with corresponding converter class that implements IMultiConverter interface.

<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox3" VerticalAlignment="Top" Width="120">
    <TextBox.Text>
          <MultiBinding Converter="{StaticResource changeConverter}">
              <Binding ElementName="textBox2" Path="Text"></Binding>
              <Binding ElementName="textBox3" Path="Text"></Binding>
          </MultiBinding>
    </TextBox.Text>
<TextBox/>

and the converter class is

public class ChangeConverter: IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                double par1 = double.Parse(values[0].ToString());
                double par2 = double.Parse(values[1].ToString());
                double res = par2 - par1;
                return res.ToString();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes,
               object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("Cannot convert back");
        }
    }

Thanks for everyone who helped! I hope this answer will be helpfull in future for others.