I am making a windows phone application that has a price field. As soon as the user starts to type I want update some other textboxes. I am using mvvm light so usually the property is not updated till the user leaves the textbox.
This does not work for me so I found this and implemented it but not I have a weird problem and I don't get why.
When I type in the box 50, the property first gets updated to "5" and then to "50" which is expected but when I first "." nothing is triggered, then when I type in 5 the property seems to be hit 3 times and once it is done it moves the cursor back to the begginging of the textbox.
So instead of 0.59 I get 90.5
Code Behind
private void txtPrice_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
TextBox textBox = sender as TextBox;
textBox.Text = "";
}
private void txtPrice_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
// Update the binding source
BindingExpression bindingExpr = textBox.GetBindingExpression(TextBox.TextProperty);
bindingExpr.UpdateSource();
}
In Model
/// <summary>
/// The <see cref="Price" /> property's name.
/// </summary>
public const string PricePropertyName = "Price";
private decimal price = 0.00M;
/// <summary>
/// Sets and gets the Price property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public decimal Price
{
get
{
return price;
}
set
{
if (price == value)
{
return;
}
RaisePropertyChanging(() => Price);
price = value;
RaisePropertyChanged(() => Price);
}
}
XAML
<TextBox x:Name="txtPrice" Margin="157,16,102,0" TextWrapping="Wrap" VerticalAlignment="Top" InputScope="Number" Text="{Binding WeightConversion.Price, Mode=TwoWay, UpdateSourceTrigger=Explicit}" Tap="txtPrice_Tap" TextChanged="txtPrice_TextChanged" />