为什么IDataErrorInfo在应用启动时显示错误 [英] Why IDataErrorInfo is showing errors at app startup

查看:217
本文介绍了为什么IDataErrorInfo在应用启动时显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ViewModel中实现IDataErrorInfo.

I'm implementing IDataErrorInfo in my ViewModel.

我有两个属性'Nom'&我想强制使用"Prenom"

I have two properties 'Nom' & 'Prenom', which I want to make mandatory

    #region IDataErrorInfo

    string IDataErrorInfo.Error
    {
        get { return null; }
    }

    string IDataErrorInfo.this[string propertyName]
    {
        get { return GetValidationError(propertyName); }
    }

    #endregion IDataErrorInfo

    #region Validation

    private static readonly string[] ValidatedProperties = { "Nom", "Prenom" };

    public bool IsValid
    {
        get
        {
            foreach (string property in ValidatedProperties)
                if (GetValidationError(property) != null)
                    return false;

            return true;
        }
    }

    private string GetValidationError(string propertyName)
    {
        string error = null;

        switch (propertyName)
        {
            case "Nom":
                error = ValidateNom();
                break;

            case "Prenom":
                error = ValidatePrenom();
                break;
        }

        return error;
    }

    private string ValidateNom()
    {
        if (string.IsNullOrWhiteSpace(Nom))
        {
            return "Last name is mandatory";
        }

        return null;
    }

    private string ValidatePrenom()
    {
        if (string.IsNullOrWhiteSpace(Prenom))
        {
            return "First name is mandatory";
        }

        return null;
    }

我这样绑定文本框的文本属性:

I'm binding the Text attribut of my TextBox like this :

<TextBox Text="{Binding Nom,
                        ValidatesOnDataErrors=True,
                        UpdateSourceTrigger=LostFocus,
                        NotifyOnValidationError=True}" />

我的问题:在失去焦点之前,文本框显示错误(在应用启动时).

My problem is : The textboxes are showing error (in the app startup) before losing focus.

我正在执行此操作(在click事件中),因此它应在单击后而不是之前显示错误:

I'm doing this (in the click event) so it should show the error after the click and not before :

if (!IsValid)
    return;

推荐答案

那不是正常行为,因为空字段未通过验证吗?您可能需要使用UpdateSourceTrigger=Explicit使其按您希望的方式进行.

Isn't that normal behaviour because the empty fields are failing validation? You might need to use UpdateSourceTrigger=Explicit to make this work the way you want it to.

评论中的空间不足时,我必须使用答案

I had to use an answer as I ran out of space in the comments

这就是问题...如果在IDataErrorInfo索引器方法中放置一个断点(与ICommandCanExecute处理程序相同),然后尝试返回到应用程序,则该断点会立即受到打击...这将在您尝试返回到应用程序时每隔 次发生.

Here's the thing... if you put a break point in your IDataErrorInfo indexer method (it's the same with the CanExecute handler of an ICommand) and then try to return to the application, the break point will immediately get hit... and this will happen every time you try to return to the application.

该框架并不总是知道何时检查这些内容,但是始终会在启动时对其进行检查...考虑一下-在大多数情况下,这正是我们想要的.不幸的是,您这次不希望这样做.

The Framework doesn't always know when to check these things, but they will always be checked on start up... think about it - for most situations, that's exactly what we want. It's just unfortunate that you don't want that this time.

我个人还是喜欢这种方式……它称为预测验证或类似的东西,它允许用户知道在尝试输入数据之前必须填写的内容.当然,这比在字段中输入值,单击保存按钮的旧系统要好得多,只是告知您某个地方有错误.因此,您可以修复该错误,然后尝试再次保存,然后得知另一个错误,等等.

Personally, I prefer it this way anyway... it's called predictive validation or something like that and it allows users to know what they have to fill in before they try to enter the data. Surely this is much better than the old system of entering values in fields, clicking on a save button, only to be told that you have an error somewhere. So you fix that error and try to save again and then get told of another error, etc.

这篇关于为什么IDataErrorInfo在应用启动时显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆