0

I'm working on a simple WPF currency converter program which has to convert Dollars to Euros and vice versa. I am using IValueConverter for tow TextBoxes on a single Property. This works, however whenever I change one textbox, it will convert the other and then convert that value back to the original textbox. How do I prevent updating the source of the currently used/focused TextBox?

MainWindow.xaml

<TextBox Name="tbDollar" Grid.Row="0" Grid.Column="1" Width="200" Height="20" Text="{Binding Price, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource converterToDollar}}"></TextBox>
<TextBox Name="tbEuro" Grid.Row="1" Grid.Column="1" Width="200" Height="20" Text="{Binding Price, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource converterToEuro}}"></TextBox>

EuroToDollarConverter.cs

public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture) {
    double price;
    if(value!=null && Double.TryParse((string)value, out price)) return price*1.137;
    return null;
}

Visual example:

currency converter issue

I've tried looking for a solution involving System.Windows.Input.FocusManager, but didn't exactly understand how I could get the contol object inside the convert method.

oddRaven
  • 672
  • 1
  • 7
  • 20
  • I think you should use two different properties for binding – Alexander Oct 12 '15 at 15:20
  • What exactly is "Price" (binding source)? – JeffRSon Oct 12 '15 at 15:33
  • Use two properties like @Alexander suggests. – Lorek Oct 12 '15 at 15:44
  • I don't think it behaves like what you described. If typing in textbox1, the `Price` will be updated with a converted value. This then shows new value in the textbox2 also via a converter. Then it stops there. It's unclear on what the underlying value of `Price` means. Is it calculated in another kind of currency or one of dollar or euro? – King King Oct 12 '15 at 16:14
  • @Lorek the value of the Price binding/property is supposed to be the value that has been put last in either one of the TextBoxes, then the other one gets the converted value from the property. – oddRaven Oct 12 '15 at 17:54
  • @KingKing see previous comment. – oddRaven Oct 12 '15 at 17:55
  • so that does not make sense, Price would be of some ***consistent*** currency, it cannot jump from dollar and then euro ..., If it's a consistent currency, such as pound, showing its value on the 2 textboxes and updating back from those controls is just fine. – King King Oct 12 '15 at 19:14
  • Yeah, your view model will have two properties. Setting Dollars will convert to and update Euros. Setting Euros will convert to and update Dollars. You will be updating the backing fields so as to not cause additional updates to the property being edited in the UI. – Lorek Oct 12 '15 at 19:15
  • This also means that your conversion code will be called from the view model and you will need to modify your bindings a bit. It will simplify your bindings. – Lorek Oct 12 '15 at 19:28
  • @oddRaven. I reproduced your code and made it to work. I just had to change the converter code because I receive a double instead of a string in hte convert method. Ok, Both textboxes are updated, and actually, there is no problem around this, no graphical flickering,no infite loop or recursion ... or what is the problem ? – Emmanuel DURIN Oct 13 '15 at 14:54

0 Answers0