gpt4 book ai didi

wpf - 检测 WPF 验证错误

转载 作者:行者123 更新时间:2023-12-03 04:39:21 25 4
gpt4 key购买 nike

在 WPF 中,您可以使用 ExceptionValidationRuleDataErrorValidationRule 根据数据绑定(bind)期间数据层中抛出的错误来设置验证。

假设您以这种方式设置了一堆控件,并且有一个“保存”按钮。当用户单击“保存”按钮时,您需要确保在继续保存之前没有验证错误。如果存在验证错误,您想对它们大喊大叫。

在 WPF 中,如何查明任何数据绑定(bind)控件是否设置了验证错误?

最佳答案

这篇文章非常有帮助。感谢所有贡献者。这是一个您可能会喜欢或讨厌的 LINQ 版本。

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 - 检测 WPF 验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/127477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com