gpt4 book ai didi

wpf - 如何正确实现 INotifyDataErrorInfo?

转载 作者:行者123 更新时间:2023-12-04 18:20:10 24 4
gpt4 key购买 nike

我有点困惑 MSDN example .

目前尚不清楚如何处理和设置实体相关错误。

示例代码:

public System.Collections.IEnumerable GetErrors(string propertyName)
{
if (String.IsNullOrEmpty(propertyName) ||
!errors.ContainsKey(propertyName)) return null;
return errors[propertyName];
}

但是 GetErrors() 的文档指出:

propertyName - The name of the property to retrieve validation errors for; or null or Empty, to retrieve entity-level errors.



另一个例子建议只返回字典的 _errors.Values。这只是所有属性错误,但又不是实体错误。

最佳答案

根据文档中的“备注”部分:MSDN: INotifyDataErrorInfo Interface

This interface enables data entity classes to implement custom validation rules and expose validation results asynchronously. This interface also supports custom error objects, multiple errors per property, cross-property errors, and entity-level errors. Cross-property errors are errors that affect multiple properties. You can associate these errors with one or all of the affected properties, or you can treat them as entity-level errors. Entity-level errors are errors that either affect multiple properties or affect the entire entity without affecting a particular property.



我可能会建议执行 GetErrors高度依赖于您的错误处理方案。例如,如果您不打算支持 Entity-Level错误,那么您的示例代码就足够了。但是,如果您确实需要支持 Entity-Level错误,那么你可以处理 IsNullOrEmpty单独条件:
Public IEnumerable GetErrors(String propertyName)
{
if (String.IsNullOrEmpty(propertyName))
return entity_errors;
if (!property_errors.ContainsKey(propertyName))
return null;
return property_errors[propertyName];
}

关于wpf - 如何正确实现 INotifyDataErrorInfo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15874453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com