在App.xaml.cs
中我已经注册了验证错误事件处理程序,以便在发生验证错误时将所有TextBox
控件还原为以前的值(VM属性中的值)(例如,如果{{1}绑定到double属性输入一个字符串值)。
TextBox
这很好用。但是当我输入合法值时,控件周围的红线仍然存在并且永远不会被删除。如何强制更新验证,以便删除红线?
答案 0 :(得分:1)
你只需要清洁"通过整理Validation.ErrorTemplate方法,TextBox的SetErrorTemplate属性的值:
private void TextBox_ValidationErrorEventHandler(object sender, RoutedEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb != null)
{
DependencyProperty prop = TextBox.TextProperty;
BindingExpression binding = BindingOperations.GetBindingExpression(tb, prop);
if (binding != null)
{
binding.UpdateTarget();
Validation.SetErrorTemplate(tb, null);
}
}
}
我希望它可以帮到你。