0

On my class I am implementing INotifyDataErrorInfo and it is working fine when validation error happens. It is putting that red frame around the TextBox, but it is not getting rid of it when validation error is fixed.

Here is my code:

    string IDataErrorInfo.this[string columnName]
    {
        get
        {
            string error = null;
            if (Context != null)
            {
                var messages = //GetMessages(); messages are validation errors
                if (messages.Count != 0)
                {
                    error = "This is critical error. Must be fixed";
                    AddError(columnName, "Some kind of error happend", false);
                }
                else
                {
                    RemoveError(columnName);
                }
            }
            return error;
        }
    }

    //I call this method to check for validation errors.
    public void CheckValidationErrors(string propertyName)
    {
        var error = this as IDataErrorInfo;
        string message = error[propertyName];

    }


    private Dictionary<String, List<String>> errors =
        new Dictionary<string, List<string>>();

    public void AddError(string propertyName, string error, bool isWarning)
    {
        if (!errors.ContainsKey(propertyName))
            errors[propertyName] = new List<string>();

        if (!errors[propertyName].Contains(error))
        {
            if (isWarning) errors[propertyName].Add(error);
            else errors[propertyName].Insert(0, error);
            RaiseErrorsChanged(propertyName);
        }
    }

    public void RemoveError(string propertyName, string error="")
    {
        if (error == "")
        {
            errors.Remove(propertyName);
            RaiseErrorsChanged(propertyName);
        }
        else
        {
            if (errors.ContainsKey(propertyName) &&
                errors[propertyName].Contains(error))
            {
                errors[propertyName].Remove(error);
                if (errors[propertyName].Count == 0) errors.Remove(propertyName);
                RaiseErrorsChanged(propertyName);
            }
        }
    }

    public void RaiseErrorsChanged(string propertyName)
    {
        if (ErrorsChanged != null)
            ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
    }


    public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

    public System.Collections.IEnumerable GetErrors(string propertyName)
    {
        if (String.IsNullOrEmpty(propertyName) ||
            !errors.ContainsKey(propertyName)) return null;
        return errors[propertyName];
    }

    public bool HasErrors
    {
        get { return errors.Count > 0; }
    }

I am calling RemoveError() method to remove errors. Am I doing something wrong? When validation fixed I need to go to TextBox and tabbing of will take care of it. I want to remove that red frame right away when validation error is gone.

UPDATE:

When I type something to text box I need to send that info to the server asynchronously and response will bring me a message if there is any validation issues. So I can't do validation things on property changed. I will check for response if there any message added or removed. If any removed then I will call CheckValidationErrors().

ANSWER

I was implementing IDataErrorInfo and then decided to implement INotifyDataErrorInfo. I was thinking to make this validation work I need to implement both interfaces. So basically I removed IDataErrorInfo implementation from my class and that fixed the problem.

Thanks for the help!

4

2 回答 2

1

Default value of UpdateSourceTrigger for TextBox is LostFocus.

You should change it to PropertyChanged if you want to run your validation logic right away once error is fixed.

<TextBox Text="{Binding PropertyName, UpdateSourceTrigger=PropertyChanged}"/>
于 2013-11-07T18:55:49.077 回答
1

I was implementing IDataErrorInfo and then decided to implement INotifyDataErrorInfo. I was thinking to make this validation work I need to implement both interfaces. So basically I removed IDataErrorInfo implementation from my class and that fixed the problem.

于 2014-07-19T11:58:04.010 回答