如何为多个 BO 属性定义 IDataErrorInfo 错误属性
我开始通过 IDataErrorInfo 接口在我的 WPF 项目中实现验证。 我的业务对象包含多个带有验证信息的属性。如何获取与该对象关联的所有错误消息的列表。我的想法是,这就是 Error 属性的用途,但我无法追踪任何使用它来报告多个属性的人。
谢谢!
public string this[string property]
{
get {
string msg = null;
switch (property)
{
case "LastName":
if (string.IsNullOrEmpty(LastName))
msg = "Need a last name";
break;
case "FirstName":
if (string.IsNullOrEmpty(LastName))
msg = "Need a first name";
break;
default:
throw new ArgumentException(
"Unrecognized property: " + property);
}
return msg;
}
}
public string Error
{
get
{
return null ;
}
}
I'm starting to implement validation in my WPF project via the IDataErrorInfo interface.
My business object contains multiple properties with validation info. How do I get a list of ALL the error messages associated with the object. My thought is that thats what the Error property is for, but I cannot track down anyone using this for reporting on multiple properties.
Thanks!
public string this[string property]
{
get {
string msg = null;
switch (property)
{
case "LastName":
if (string.IsNullOrEmpty(LastName))
msg = "Need a last name";
break;
case "FirstName":
if (string.IsNullOrEmpty(LastName))
msg = "Need a first name";
break;
default:
throw new ArgumentException(
"Unrecognized property: " + property);
}
return msg;
}
}
public string Error
{
get
{
return null ;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为使用验证属性要容易得多。
辅助类 InputValidation 看起来像这样
I think it is much easier to use the Validation attributes.
The helper class InputValidation looks like this
是的,我知道你可以在哪里使用索引器。我想这不是一个坏方法。不过,我真正关注的是“错误”属性。我喜欢将错误包含在业务对象中的概念。我认为我想要做的事情本身并不存在,所以我只是在对象上创建了一个错误字典(在属性更改时更新),并让错误返回回车分隔的错误列表,如下所示:
Yeah, I see where you could use the indexer. Not a bad way to go I guess. I was really focused on the 'Error' property though. I like the notion of having the errors contained within the business object. I think what I want to do doesnt exist natively, so I just created a dictionary of errors (updated anytime a property changes) on the object and let the Error return a CarriageReturn delimited list of errors, like so :
我的理解是,要使用此接口,您需要枚举对象的属性,并为每个属性调用一次索引器。调用者有责任聚合所有错误消息。
My understanding is that to use this interface, you enumerate the properties on the object, and call the indexer once for each property. It is the caller's responsibility to aggregate any error messages.