如果在扩展器内的控件中发生IDataError验证,是否有人知道更改扩展器样式的方法。例如。

<Expander Header="Details">
    <TextBox Text="{Binding Brand.DESCRIPTION,
                            UpdateSourceTrigger=LostFocus,
                            ValidatesOnDataErrors=True}"/>
</Expander>


因此,如果文本框有错误,我的扩展器的样式将会更改(可能会变成红色)。
我正在寻求使它尽可能通用,以便在可能的情况下不手动绑定到扩展器中的每个控件。

最佳答案

您可以通过“附加行为”使用附加事件Validation.Error(每次添加或删除验证错误时都会引发)。要使此工作有效,您需要在绑定中添加NotifyOnValidationError=True

如果在绑定上将ChildValidation设置为True,则此附加行为Validation.Error订阅ExpanderNotifyOnValidationError事件,该事件会冒泡。由于Control中可能有多个Expander,因此还需要跟踪当前处于活动状态的“验证错误”计数,以确定是否应显示红色边框。它可能看起来像这样

Xaml

<Expander Header="Details"
            behaviors:ChildValidationBehavior.ChildValidation="True">
    <TextBox Text="{Binding Brand.DESCRIPTION,
                            UpdateSourceTrigger=LostFocus,
                            ValidatesOnDataErrors=True,
                            NotifyOnValidationError=True}"/>
</Expander>


儿童验证行为

public static class ChildValidationBehavior
{
    private static readonly DependencyProperty ErrorCountProperty =
        DependencyProperty.RegisterAttached("ErrorCount",
                                            typeof(int),
                                            typeof(ChildValidationBehavior));
    private static void SetErrorCount(DependencyObject element, int value)
    {
        element.SetValue(ErrorCountProperty, value);
    }
    private static int GetErrorCount(DependencyObject element)
    {
        return (int)element.GetValue(ErrorCountProperty);
    }

    public static readonly DependencyProperty ChildValidationProperty =
        DependencyProperty.RegisterAttached("ChildValidation",
                                            typeof(bool),
                                            typeof(ChildValidationBehavior),
                                            new UIPropertyMetadata(false, OnChildValidationPropertyChanged));
    public static bool GetChildValidation(DependencyObject obj)
    {
        return (bool)obj.GetValue(ChildValidationProperty);
    }
    public static void SetChildValidation(DependencyObject obj, bool value)
    {
        obj.SetValue(ChildValidationProperty, value);
    }
    private static void OnChildValidationPropertyChanged(DependencyObject dpo,
                                                         DependencyPropertyChangedEventArgs e)
    {
        Control control = dpo as Control;
        if (control != null)
        {
            if ((bool)e.NewValue == true)
            {
                SetErrorCount(control, 0);
                Validation.AddErrorHandler(control, Validation_Error);
            }
            else
            {
                Validation.RemoveErrorHandler(control, Validation_Error);
            }
        }
    }
    private static void Validation_Error(object sender, ValidationErrorEventArgs e)
    {
        Control control = sender as Control;
        if (e.Action == ValidationErrorEventAction.Added)
        {
            SetErrorCount(control, GetErrorCount(control)+1);
        }
        else
        {
            SetErrorCount(control, GetErrorCount(control)-1);
        }
        int errorCount = GetErrorCount(control);
        if (errorCount > 0)
        {
            control.BorderBrush = Brushes.Red;
        }
        else
        {
            control.ClearValue(Control.BorderBrushProperty);
        }
    }
}

关于c# - WPF扩展器验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4936591/

10-12 12:44