INotifyDataErrorInfo不引发错误,其背后的代码已更改 [英] INotifyDataErrorInfo not raising error changed in code behind
问题描述
我正在从后面的代码中获得发布的执行验证.我的数据显示在数据网格中.列(类型)之一是下拉菜单,更改下拉菜单后会触发DropDownClosed事件,该事件将在后面的代码中处理.
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.
我要实现的是验证以下列的内容以匹配下拉列表中新选择的类型.如果不匹配,我希望在网格上显示验证错误.我使用INotifyDataErrorInfo接口实现了验证,除非我在后面的代码中使用它,否则它确实运行良好.当后面的代码调用验证时,永远不会更新datagrid的ValidationSummary.我在这里做错了什么?使用调试器时,我可以清楚地看到错误已添加到接口的错误"词典中.
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...
这里是处理程序:
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");
}
}
validate规则方法将基于ruletypevalue调用此方法
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;
}
我遵循了MSDN上的示例实现 http://msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifydataerrorinfo%28VS.95%29.aspx
I followed the sample implementation on MSDN http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifydataerrorinfo%28VS.95%29.aspx
推荐答案
早些时候,我已经实现了验证帮助器,并为接口IDataErrorInfo
和INotifyDataErrorInfo
创建了示例解决方案:
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/
主要实现在这里:
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);
}
}
无论验证是否成功,都应始终调用OnErrorsChanged
(或您的情况下的RaiseErrorsChanged
)方法:如果属性无效-将显示红色边框,如果有效-绑定控件将为回到正常状态.
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.
这篇关于INotifyDataErrorInfo不引发错误,其背后的代码已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!