我正在开发我的 wpf 应用程序并使用 INotifyDataErrorInfo 进行验证。我有两个类(A & B),其中一个(A)有另一个(B)的两个实例。现在我想从 A 验证 B 的属性。这可能使用 INotifyDataErrorInfo 吗?
这是我的示例代码:
class BaseValidation : INotifyDataErrorInfo, INotifyPropertyChanged
{
// the implementaion of interface
public void MyValidationMethod(string propertyName, Func<bool> expression, string errorMessage)
{
}
}
class B : BaseValidation
{
public string MyString
{
get {return _myString;}
set {_myString = value;}
}
}
class A : BaseValidation
{
public B objB1;
public B objB2
A(){
objB1 = new B();
objB1.PropertyChanged += OnObjBPropertyChanged;
objB2 = new B();
}
private void OnObjBPropertyChanged(object sender, PropertyChangedEventArgs arg)
{
MyValidationMethod("objB1.MyString ", () => objB1.MyString != objB2.MyString , "Error");
// here validation will pass if MyString are not equal
}
}
我觉得我在验证方法中传递了错误的属性名称。而且我无法在内部实现此验证,B
因为我需要其他B
对象的数据。我会假设这种类型的验证是可能的,因为我可以假设这是一种常见的情况,我该怎么做?