从代码后台进行此操作会更加棘手。基本上,您可以从代码中设置一个临时绑定,引发验证错误,当输入具有有效值时,您可以再次删除所有临时绑定内容。
这是我使用的方法,我认为这是一种不好的做法(但总比没有好):
public static void ValidationMarkInvalid(TextBox textBox, String errorContent)
{
DependencyProperty textProp = TextBox.TextProperty;
if (!BindingOperations.IsDataBound(textBox, textProp))
{
if (textBox.DataContext == null)
{
textBox.DataContext = new EmptyDataContext();
}
Binding b = new Binding("CodeBehind");
b.FallbackValue = textBox.Text;
b.ValidatesOnExceptions = true;
BindingOperations.SetBinding(textBox, textProp, b);
}
BindingExpression bindingInError =
textBox.GetBindingExpression(TextBox.TextProperty);
var validationError = new ValidationError(
new EmptyValidationRule(),
bindingInError,
errorContent,
new Exception(errorContent));
Validation.MarkInvalid(bindingInError, validationError);
}
public static void ValidationClear(TextBox textBox)
{
DependencyProperty textProp = TextBox.TextProperty;
if (BindingOperations.IsDataBound(textBox, textProp))
{
String value = textBox.Text;
Validation.ClearInvalid(textBox.GetBindingExpression(TextBox.TextProperty));
BindingOperations.ClearBinding(textBox, textProp);
textBox.Text = value;
EmptyDataContext ctx = textBox.DataContext as EmptyDataContext;
if (ctx != null)
{
textBox.DataContext = null;
}
}
}
#region Nested Type: EmptyDataContext
private sealed class EmptyDataContext : INotifyPropertyChanged
{
public Object CodeBehind
{
get
{
throw new FormatException();
}
set
{
throw new FormatException();
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
#endregion
#region Nested Type: EmptyValidationRule
private sealed class EmptyValidationRule : ValidationRule
{
public override ValidationResult Validate(Object value, CultureInfo cultureInfo)
{
return new ValidationResult(false, String.Empty);
}
}
#endregion
现在,你可以在代码后端这样做:
ValidationMarkInvalid(textBox, "Please enter something valid!");
并清除验证:
ValidationClear(textBox);
P.S.: 如果您不想在异常上进行验证,可以从上述方法中删除EmptyDataContext类。