【问题标题】:WPF ItemsControl ItemTemplate ValidationWPF ItemsControl ItemTemplate Validation
【发布时间】:2011-07-13 00:23:01
【问题描述】:

有没有办法确定 ItemsControl 是否有任何带有验证错误的子控件?我想将此值(布尔值)绑定到按钮上的 IsEnabled 属性。

【问题讨论】:

    标签: wpf validation data-binding itemscontrol


    【解决方案1】:

    我最近使用了上一个 SO 答案中的代码 Detecting WPF Validation Errors 来解决这个问题。

    我有一个包含 DataGrid 的用户控件。用户控件公开了一个 IsValid 属性,其中包含一个 getter,它只是调用作为 DependencyObject 传入 DataGrid 的静态 IsValid 函数:

    public class MyControl : UserControl
    {
        public bool IsValid
        {
            get { return Validator.IsValid(MyDataGrid); }
        }
    }
    

    然后可以通过绑定到要启用/禁用按钮的命令的 CanExecute 函数检查控件的 IsValid 属性。

    我链接到的代码的唯一问题是它实际上评估绑定的验证,这意味着一旦您运行它,任何在技术上无效但尚未失效的字段(即用户可能尚未在该字段中输入任何数据,因为他们没有得到它)​​现在将处于无效状态 - 我还没有寻找避免或减轻这种情况的方法。


    编辑:

    这是一个更新版本,它不会像我之前提到的那样使控件无效。我只是简单地注释掉/稍微更改了一些行,但将所有内容都留在了那里,以便您可以看到不同之处。请注意,这也应该执行得更快,因为您将在找到第一个无效绑定的那一刻退出。

    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;
                        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))
            {
                //valid = false;
                return false;
            }
        }
    
        //return valid;
        return true;
    }
    

    【讨论】:

    • 这就是我要找的。我唯一希望的另一件事是绑定结果的方法。
    【解决方案2】:

    我不知道为什么,slugter 的回答对我不起作用(LocalValueEnumerator 返回了一些属性,但从未返回绑定的属性,例如 Text)。

    我设法让它使用这段代码(源自this answer):

    public static 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) &&
            GetVisualTreeChildren(obj)
            .OfType<DependencyObject>()
            .All(child => IsValid(child));
    }
    
    //VisualTreeHelper don't have a method to get all the children of a visual object
    private static IEnumerable GetVisualTreeChildren(DependencyObject parent)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            yield return VisualTreeHelper.GetChild(parent, i);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      相关资源
      最近更新 更多