问题描述
在WPF中,您可以使用 ExceptionValidationRule
或 DataErrorValidationRule
。
In WPF you can setup validation based on errors thrown in your Data Layer during Data Binding using the ExceptionValidationRule
or DataErrorValidationRule
.
假设你有一堆设置这样的控件,你有一个保存按钮。当用户单击保存按钮时,您需要确保在继续保存之前没有验证错误。如果有验证错误,那么你想在他们身上。
Suppose you had a bunch of controls set up this way and you had a Save button. When the user clicks the Save button, you need to make sure there are no validation errors before proceeding with the save. If there are validation errors, you want to holler at them.
在WPF中,如何看出你的数据绑定控件是否有验证错误? p>
In WPF, how do you find out if any of your Data Bound controls have validation errors set?
推荐答案
这篇文章是非常有帮助的。感谢所有贡献者。这是一个LINQ版本,你将会喜欢或讨厌。
This post was extremely helpful. Thanks to all who contributed. Here is a LINQ version that you will either love or hate.
private void CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = IsValid(sender as DependencyObject);
}
private bool IsValid(DependencyObject obj)
{
// The dependency object is valid if it has no errors and all
// of its children (that are dependency objects) are error-free.
return !Validation.GetHasError(obj) &&
LogicalTreeHelper.GetChildren(obj)
.OfType<DependencyObject>()
.All(IsValid);
}
这篇关于检测WPF验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!