Why IDataErrorInfo is showing errors at app startup

空扰寡人 提交于 2019-12-11 03:39:54

问题


I'm implementing IDataErrorInfo in my ViewModel.

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.

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

if (!IsValid)
    return;

回答1:


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

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.



来源:https://stackoverflow.com/questions/19179292/why-idataerrorinfo-is-showing-errors-at-app-startup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!