Strange order of firing of Validation.Error event - Added fired before Removed

做~自己de王妃 提交于 2019-12-06 02:14:16

Looking at the source code, the steps of adding a new validation error before removing an old one

are carefully ordered to avoid going through a "no error" state while replacing one error with another

Keeping in mind fractor's answer you can try to do a little workaround. Create some counter that will represent errors count of validated control:

int errorCounter = 0;
private void TextBox_Error(object sender, ValidationErrorEventArgs e)
{
    var tb = sender as TextBox;
    if (tb != null)
    {
        errorCounter += (e.Action == ValidationErrorEventAction.Added) ? 1 : -1;
        //here do whatever you want to with you stored information about error
        someControl.IsEnabled = !(errorCounter > 0);
    }
}

I know it's kinda old question but I hope it will help.

You can use this event to determine a change in error state, but since they appear out of order (for good reason, see fractor's answer), you should read the Validation.HasErrors property instead.

var hasErrors = (bool)GetValue(Validation.HasErrorProperty);

Do this in the same handler, it will always be correct.

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