我正在使用自定义TextBox,它需要Double值,我已经应用了一些验证,它工作正常,但是当我按BackSpace时,它也会删除惹恼的小数点。例如,如果当前值是“2.5”,并且按退格键“.5”被移除,而期望值应该是“2”。TextBox验证错误
这是其在用户控件
<rmguiutil:RMTextBox Margin="5,5,0,0" Width="40" HorizontalAlignment="Left" OnlyAllow="Double"
Text="{Binding StartConcentration, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
IsEnabled="{Binding IngredientIngredientTypeRow, Converter={StaticResource GlobalNullObjectToBooleanConverter}, FallbackValue=False}" />
这里使用的是后面的代码为我的自定义文本框我的自定义TextBox的XAML,我已重写其PreviewTextInput事件
protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
{
base.OnPreviewTextInput(e);
if(OnlyAllow == RMTextBoxOnlyAllow.Double && (e.Text.Any(c => !Char.IsDigit(c) && c != '.') || (e.Text.Count(c => c == '.') + Text.Count(c => c == '.')) > 1))
e.Handled = true;
else if(OnlyAllow == RMTextBoxOnlyAllow.Integer && e.Text.Any(c => !Char.IsDigit(c)))
e.Handled = true;
}
我可以找不到解决我的问题的方法。
这对我来说很不明显,你在做什么。正常路径是2步验证:1)当用户正在编辑值时,如果在编辑过程中double.TryParse()返回false,你可以用红色显示他。 2)当用户确认他的输入时(焦点丢失?按Enter键?),你再次执行'double.TryParse()',如果值无效,则不要改变原来的值。 – Sinatr 2014-10-02 10:18:33