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:
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.