INotifyDataErrorInfo not raising error changed in code behind

。_饼干妹妹 提交于 2019-12-13 07:08:17

问题


I am experiencing issued performing validation from the codebehind. My data is displayed in a datagrid. One of the columns (type) is a drop down and when the drop down menu is changed it triggers a DropDownClosed Event which is handled in the code behind.

What I am trying to achieve is to validate the content of the following column to match the newly selected type in the drop down. If it does not match i want a validation error to be displayed on the grid. I implemented my validation using the INotifyDataErrorInfo interface and it works really well except when I use it in the code behind. When the code behind calls the validation the ValidationSummary of the datagrid is never updated. What I am doing wrong here ??? When using the debugger I can clearly see the errors being added to the Errors dictionnary of the interface...

Here is the handler:

        private void TypeBoxChanged(object sender, EventArgs e)
        {
        ComboBox box = (sender as ComboBox);
        IncomingPolicy row = (IncomingPolicy)box.DataContext;

        string ruleTypeValue = TypeList.GetKeyForText(box.SelectedItem.ToString());
        //check if the type is the same
        if(row.TypeWrapper == ruleTypeValue)
            return;
        if (row.ValidateRule(ruleTypeValue))
        {
            //SAVE the record
        }
        else
        {
            row.RaiseErrorsChanged("RuleWrapper");
        }
    }

The validate rule method will based on the ruletypevalue call this method

        public bool ValidateRegularExpression(string property, string value, string expression, string errorMessage)
        {
        bool isValid = true;
        Regex regex = new Regex(expression);
        Match match = regex.Match(value);
        if (match.Success)
        {
            RemoveError(property, errorMessage);                
        }
        else
        {
            AddError(property, errorMessage, false);
            isValid = false;
        }

        return isValid;
    }

I followed the sample implementation on MSDN http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifydataerrorinfo%28VS.95%29.aspx


回答1:


Some time earlier I've implemented validation helpers and created the sample solution for both interfaces IDataErrorInfo and INotifyDataErrorInfo:

http://vortexwolf.wordpress.com/2011/10/01/wpf-validation-with-idataerrorinfo/

Source code

The main implementation is here:

this.PropertyChanged += (s, e) => 
{
    // if the changed property is one of the properties which require validation
    if (this._validator.PropertyNames.Contains(e.PropertyName))
    {
        this._validator.ValidateProperty(e.PropertyName);
        OnErrorsChanged(e.PropertyName);
    }
}

You should always call the OnErrorsChanged (or RaiseErrorsChanged in your case) method regardless of success of validation: if the property is invalid - the red border will be displayed, if it is valid - the bound control will be returned to its normal state.



来源:https://stackoverflow.com/questions/9670118/inotifydataerrorinfo-not-raising-error-changed-in-code-behind

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