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!

1
  • Dilshod, please add your answer into an answer and mark it as accepted, so that this question can be marked as answered. Commented Jul 18, 2014 at 21:20

2 Answers 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}"/>
Sign up to request clarification or add additional context in comments.

1 Comment

Lets say I typed "Test" and I don't get the error right away. It send the data to the server asynchronously and it will return message if there is a validation error. And I call manually check for validation errors. So now when I type a valid text to a textBox also sends it to server async. the server removes the message. After that I am checking for validation error.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.