Je voudrais aller avec un comportement et une regex. Ensuite, vous pouvez facilement réutiliser votre code pour d'autres zones de texte.
public class RegexValidationBehavior : Behavior<TextBox>
{
public static readonly DependencyProperty RegexStringProperty =
DependencyProperty.Register("RegexString", typeof(string), typeof(RegexValidationBehavior), new PropertyMetadata(string.Empty));
public string RegexString
{
get { return GetValue(RegexStringProperty) as string; }
set { SetValue(RegexStringProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
if (AssociatedObject != null)
{
AssociatedObject.TextChanged += OnTextChanged;
}
Validate();
}
protected override void OnDetaching()
{
base.OnDetaching();
if (AssociatedObject != null)
{
AssociatedObject.TextChanged -= OnTextChanged;
}
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
Validate();
}
private void Validate()
{
var value = AssociatedObject.Text;
if (value.IsNotEmpty() && RegexString.IsNotEmpty())
{
MatchAgainstRegex(value);
}
}
private void MatchAgainstRegex(string value)
{
var match = Regex.Match(value, RegexString);
if (!match.Success)
{
AssociatedObject.Text = value.Remove(value.Length - 1);
AssociatedObject.Select(AssociatedObject.Text.Length, 0);
}
}
}
Ensuite, dans votre XAML, écrivez quelque chose comme.
<TextBox InputScope="Number" Text="{Binding Amount, Mode=TwoWay}">
<i:Interaction.Behaviors>
<Control:RegexValidationBehavior RegexString="{Binding OnlyTwoDecimalsRegex}"/>
</i:Interaction.Behaviors>
</TextBox>
Dans votre ViewModel vous spécifiez un Regex, par exemple
public string OnlyTwoDecimalsRegex { get { return @"^([0-9]+)?([,|\.])?([0-9]{1,2})?$"; } }
Valider l'entrée? – vcsjones