绑定上的WPF验证 - ComboBox SelectedItem将不会验证

时间:2009-07-01 14:19:07

标签: wpf validation binding

我尝试为ComboBox编写自己的验证规则,规则附加到SelectedItem的绑定 - 但它不起作用。我在Text属性上有类似的规则......

<ComboBox VerticalAlignment="Top" ItemsSource="{Binding Animals}" DisplayMemberPath="Name" >
        <ComboBox.SelectedItem>
            <Binding Path="Animal">
                <Binding.ValidationRules>
                    <validators:ComboBoxValidationRule ErrorMessage="Please select an animal" />
                </Binding.ValidationRules>
            </Binding>
        </ComboBox.SelectedItem>
    </ComboBox>

我认为它归结为我用来调用我在网上找到的验证的代码。基本上SelectedItem不会出现依赖属性。

它通过依赖包含TextProperty和SelectionBoxItemProperty但没有SelectedItemProperty的dependencyPropertyField来实现。

private void ValidateBindings(DependencyObject element)
    {
        Type elementType = element.GetType();

        FieldInfo[] dependencyPropertyFields = elementType.GetFields(
            BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly);


        // Iterate over all dependency properties
        foreach (FieldInfo dependencyPropertyField in dependencyPropertyFields)
        {
            DependencyProperty dependencyProperty =
                dependencyPropertyField.GetValue(element) as DependencyProperty;

            if (dependencyProperty != null)
            {


                Binding binding = BindingOperations.GetBinding(element, dependencyProperty);


                BindingExpression bindingExpression = BindingOperations.GetBindingExpression(element, dependencyProperty);

                // Issue 1822 - Extra check added to prevent null reference exceptions
                if (binding != null && bindingExpression != null)
                {


                    // Validate the validation rules of the binding
                    foreach (ValidationRule rule in binding.ValidationRules)
                    {
                        ValidationResult result = rule.Validate(element.GetValue(dependencyProperty),
                            CultureInfo.CurrentCulture);

                        bindingExpression.UpdateSource();

                        if (!result.IsValid)
                        {
                            ErrorMessages.Add(result.ErrorContent.ToString());
                        }

                        IsContentValid &= result.IsValid;
                    }
                }
            }
        }
    }

无论如何都知道我哪里出错了?

任何帮助都非常感谢!

谢谢,

安迪

1 个答案:

答案 0 :(得分:3)

您没有找到SelectedItemProperty,因为ComboBox doesn't have是一个SelectedItemProperty字段,而是从它的基类Selector继承它。当然,Selector和ComboBox也没有你可能绑定的所有属性,你必须一直遍历到UIElement才能找到大部分继承的属性。

如果您插入某些东西来遍历继承层次结构,那么您可以获取所有字段,并且将触发验证规则。

List<FieldInfo> dependencyPropertyFields = elementType.GetFields(
    BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly).ToList();

// Iterate through the fields defined on any base types as well
Type baseType = elementType.BaseType;
while (baseType != null)
{
    dependencyPropertyFields.AddRange(
        baseType.GetFields(
            BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly));

    baseType = baseType.BaseType;
}