I'm having an issue I need to work around. The caret position of my TextBox is reset to the first character when certain events happen. Here's the XAML for my TextBox, my application is using MVVM:
<TextBox x:Name="txtAmount" Text="{Binding CurrentClientObject.Amount, Mode=TwoWay, StringFormat='###,###,##0.00'}"></TextBox>
Is bound to model property:
private System.Nullable<decimal> _Amount;
[Display(ResourceType = typeof(MatchModelResx), Name = "LabelAmount", Description = "ToolTipAmount")]
public System.Nullable<decimal> Amount
{
get
{
return _Amount;
}
set
{
_Amount = value;
NotifyChanged("Amount");
}
}
The StringFormat is causing the issue here, since whenever the string I input in the TextBox triggers a refresh of the property in the model, the StringFormat is applied and the caret inside the TextBox moves
Now this wouldn't be a problem, since the model is refreshed on the lostfocus (UpdateSourceTrigger = Default), but there is a case when the TextBox is in Error mode. If the user either enters a value that breaks a custom validation rule, or is in the invalid format (i.e: enters 64.5x5 in a field bound to a decimal), every single keychange triggers a refresh of the property in the model. And since a model refresh may cause the StringFormat to be applied, this means the caret will move to the left while a user is trying to correct an invalid value in the TextBox. This is an irritating behaviour for our customers, and we really need to find a way around it.
I've tried using a custom converter on the binding instead of a stringformat, but that's not fixing the issue. And since the exception is sometimes raised before the code reaches the property Set, I have no way to handle this before the caret moves.
Does Silverlight 5 offer a way to work around this?
Thanks!