Hodaya Shalom
Hodaya Shalom

Reputation: 4417

Validation.Error event giving strange behavior

I use the wpf event called: Validation.Error to know in cs code when there is a validation error on the control.

When the error is created the first time he comes to the event with ValidationErrorEventAction.Added.

Once there has been an error on the control, for another error is coming twice to the event:

The first time it comes with ValidationErrorEventAction.Added.

The second time it comes with ValidationErrorEventAction.Removed.

After searching I found this topic on the following question: Validation.Error giving strange behavior

Seems that when there has been an error on the control he wants to remove the error and then add a new one, the problem that it makes it in the opposite way, it adds a new one first and then remove.

Any ideas how to fix this?

Upvotes: 0

Views: 521

Answers (2)

Tuto
Tuto

Reputation: 36

I found a easier way to know if there is still a validation error:

Inside ValidationErrorEventArgs there is a variable that indicates still there is a validation error.

Example of Validation.Error event implementation with this issue fixed:

    private void TextBox_Error(object sender, ValidationErrorEventArgs e)
    {
        if (e.Action == ValidationErrorEventAction.Added)
        {
            ((Control)sender).ToolTip = e.Error.ErrorContent.ToString();
        }
        else
        {
            if (!((BindingExpressionBase)e.Error.BindingInError).HasError)
                ((Control)sender).ToolTip = "";
        }
    }

Upvotes: 0

Hodaya Shalom
Hodaya Shalom

Reputation: 4417

I found a way to know.

I keep a dictionary that contains the name of a variable and a list of errors:

 private Dictionary<string, List<ValidationError>> invalidList = new Dictionary<string, List<ValidationError>>();

Each time an error is added to the variable, I add it to the list of errors, and when the error is cleared I delete it from his errors list.

Then I can see if there is any error on the variable or do not have errors at all.

(I need to know the name of a variable, you can do either dictionary contains control or what you need)

Upvotes: 1

Related Questions