【问题标题】:IDataErrorInfo - Initially ignore the errors when view is first loadedIDataErrorInfo - 首次加载视图时最初忽略错误
【发布时间】:2012-04-28 17:30:44
【问题描述】:

我正在尝试使用 IDataErrorInfo 验证我的模型类,如下所示。

//Validators
public string this[string propertyName] {
    get {
        string error = null;

        if (propertyName == "Name") {
            error = ValidateName(); 
        }
        return error;
    }
}

这很好用,只是当视图首次加载时,它已经包含验证错误。首次加载视图时是否可以忽略/抑制验证错误。此外,在加载视图时以及在用户开始模型属性的数据输入之前显示错误是否是一种常见的做法。

问候, 涅槃。

编辑: 这就是我设置 IDataErrorInfo 的方式。

<TextBox Text="{Binding Name, ValidatesOnDataErrors=True}" Grid.Row="1" Grid.Column="1" />

【问题讨论】:

标签: wpf validation idataerrorinfo wpf-4.0


【解决方案1】:

我采用了以下方法并且它有效。基本上,模型应该正确地记录错误并将它们列在字典中,即使对象刚刚被实例化并且用户还没有输入任何文本。所以我没有更改我的模型代码或 IDataErrorInfo 验证代码。相反,我最初只是将 Validation.Error 模板属性设置为 {x:Null}。然后有代码连接 TextBox 的 LostFocus 事件,将 Validation.Error 模板更改回我正在使用的模板。为了实现模板的交换并将 LostFocus 事件处理程序附加到我的应用程序中的所有 TextBox,我使用了几个依赖项属性。这是我使用的代码。

依赖属性和 LostFocus 代码:

    public static DependencyProperty IsDirtyEnabledProperty = DependencyProperty.RegisterAttached("IsDirtyEnabled",
             typeof(bool), typeof(TextBoxExtensions), new PropertyMetadata(false, OnIsDirtyEnabledChanged));
    public static bool GetIsDirtyEnabled(TextBox target) {return (bool)target.GetValue(IsDirtyEnabledProperty);}
    public static void SetIsDirtyEnabled(TextBox target, bool value) {target.SetValue(IsDirtyEnabledProperty, value);}

    public static DependencyProperty ShowErrorTemplateProperty = DependencyProperty.RegisterAttached("ShowErrorTemplate",
             typeof(bool), typeof(TextBoxExtensions), new PropertyMetadata(false));
    public static bool GetShowErrorTemplate(TextBox target) { return (bool)target.GetValue(ShowErrorTemplateProperty); }
    public static void SetShowErrorTemplate(TextBox target, bool value) { target.SetValue(ShowErrorTemplateProperty, value); }

    private static void OnIsDirtyEnabledChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) {
        TextBox textBox = (TextBox)dependencyObject;
        if (textBox != null) {
            textBox.LostFocus += (s, e) => {
                if ((bool) textBox.GetValue(ShowErrorTemplateProperty) == false) {
                    textBox.SetValue(ShowErrorTemplateProperty, true);
                }
            };
        }
    }

如果 IsDirtyEnabled 依赖属性设置为 true,它会使用回调将 TextBox 的 LostFocus 事件附加到处理程序。处理程序只是将 ShowErrorTemplate 附加属性更改为 true,然后在 TextBox 失去焦点时触发文本框的样式触发器以显示 Validation.Error 模板。

文本框样式:

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ValidationErrorTemplate}"/>
    <Setter Property="gs:TextBoxExtensions.IsDirtyEnabled" Value="True" />
    <Style.Triggers>
        <Trigger Property="gs:TextBoxExtensions.ShowErrorTemplate" Value="false">
            <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        </Trigger>
    </Style.Triggers>
</Style>

对于一件简单的事情来说,这似乎是太多的代码,但是对于我正在使用的所有文本框,我只需执行一次。

问候, 涅槃。

【讨论】:

  • Nirvan 的解决方案可以解决问题,如果您将此代码移至基类(例如 BaseControll),则您可以从该基类派生所有控件并在所有控件中具有此行为。跨度>
  • 我知道已经有一段时间了,但我面临着同样的问题。您的解决方案完美运行,我的问题是所有字段的验证错误消息是否相同——错误模板指向静态资源——谢谢!
【解决方案2】:

尝试在视图显示后设置数据上下文。

就我而言,这很有帮助。

【讨论】:

    【解决方案3】:

    您是否在 get 中抛出异常?

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            if (String.IsNullOrEmpty(value))
            {
                throw new ApplicationException("Customer name is mandatory.");
            }
        }
    }
    

    【讨论】:

    • 不,我没有抛出任何异常。我正在使用 IDataErrorInfo 接口来验证模型属性。
    • 我记得我必须使用 ValidationRules 来仅在片场进行错误检查。抱歉,我现在无法访问该代码。
    • 感谢您的努力。
    【解决方案4】:

    您在 ValidateName() 方法中设置规则。您的视图只显示错误:) 我建议 name 是必填字段,应由用户填写,但您不喜欢第一次加载视图时的红色边框?

    我使用两种不同的控制模板进行验证。errortemplate 正常的一个和一个用于必填字段(红色 *)

    thats 我上次的做法。

    【讨论】:

    • 这肯定会在视图中隐藏错误,并且可能是一个可能的解决方案。但是,如果有比这个更好的解决方案,我仍然会等待一段时间。谢谢
    猜你喜欢
    • 2011-12-28
    • 2020-04-15
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    相关资源
    最近更新 更多