首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >正在检测WPF验证错误

正在检测WPF验证错误
EN

Stack Overflow用户
提问于 2008-09-24 14:22:11
回答 9查看 65.7K关注 0票数 118

在WPF中,您可以使用ExceptionValidationRuleDataErrorValidationRule根据在数据绑定期间在数据层中引发的错误来设置验证。

假设你有一堆这样设置的控件,并且你有一个Save按钮。当用户单击save按钮时,您需要确保在继续保存之前没有验证错误。如果存在验证错误,您需要对其进行处理。

在WPF中,如何找出是否有任何数据绑定控件设置了验证错误?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2011-01-11 02:59:57

这篇文章非常有帮助。感谢所有做出贡献的人。这是一个你会喜欢或讨厌的LINQ版本。

代码语言:javascript
复制
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);
}
票数 140
EN

Stack Overflow用户

发布于 2008-09-24 16:56:22

下面的代码(摘自Chris Sell和Ian Griffiths的Programming WPF一书)验证了依赖对象及其子对象上的所有绑定规则:

代码语言:javascript
复制
public static class Validator
{

    public static bool IsValid(DependencyObject parent)
    {
        // Validate all the bindings on the parent
        bool valid = true;
        LocalValueEnumerator localValues = parent.GetLocalValueEnumerator();
        while (localValues.MoveNext())
        {
            LocalValueEntry entry = localValues.Current;
            if (BindingOperations.IsDataBound(parent, entry.Property))
            {
                Binding binding = BindingOperations.GetBinding(parent, entry.Property);
                foreach (ValidationRule rule in binding.ValidationRules)
                {
                    ValidationResult result = rule.Validate(parent.GetValue(entry.Property), null);
                    if (!result.IsValid)
                    {
                        BindingExpression expression = BindingOperations.GetBindingExpression(parent, entry.Property);
                        System.Windows.Controls.Validation.MarkInvalid(expression, new ValidationError(rule, expression, result.ErrorContent, null));
                        valid = false;
                    }
                }
            }
        }

        // Validate all the bindings on the children
        for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);
            if (!IsValid(child)) { valid = false; }
        }

        return valid;
    }

}

您可以在保存按钮单击事件处理程序中调用此函数,如下所示

代码语言:javascript
复制
private void saveButton_Click(object sender, RoutedEventArgs e)
{

  if (Validator.IsValid(this)) // is valid
   {

    ....
   }
}
票数 50
EN

Stack Overflow用户

发布于 2009-02-19 14:31:21

在使用ListBox时,发布的代码对我不起作用。我重写了它,现在它可以工作了:

代码语言:javascript
复制
public static bool IsValid(DependencyObject parent)
{
    if (Validation.GetHasError(parent))
        return false;

    // Validate all the bindings on the children
    for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i)
    {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        if (!IsValid(child)) { return false; }
    }

    return true;
}
票数 34
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/127477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档