【发布时间】:2013-01-11 15:28:14
【问题描述】:
我在试图找出在我的 WPF 应用程序中实现 IDataErrorInfo 的最佳方法时遇到了麻烦。我有几个模型,每个模型都有很多属性,正在使用中。 ViewModel 具有每个模型的属性,视图使用这些属性绑定到模型。这是我所拥有的结构的简化版本:
public class ModelA
{
public string PropertyA1 {get; set;}
public string PropertyA2 {get; set;}
}
public class ModelB
{
public string Property B1 {get; set;}
public string Property B2 {get; set;}
}
public class ViewModel
{
public ModelA modelA {get; set;}
public ModelB modelB {get; set;}
}
我的问题是 - 我在哪里实现 IDataErrorInfo - 在 ViewModel 中还是在模型中? View 以 modelA.PropertyA1 等方式绑定到这些属性,因此在模型中而不是在 ViewModel 中引发错误,这使得有必要在模型中实现 IDataErrorInfo。不过,我听说在 ViewModel 中实现验证逻辑是一种很好的做法。
我想知道是否有一种方法可以捕获 ViewModel 中的错误,而无需为每个会引发错误的属性编写包装器,因为有很多属性并且为每个属性编写包装器会很乏味。
感谢您的帮助!
【问题讨论】:
标签: wpf idataerrorinfo