我想在用户输入数据库表 itemMaster 时验证数据网格单元格,即 Item 列。我怀疑哪种方法最适合我验证输入,ValidationRule 或 IDataErrorInfo?
1 回答
This question is 100% a duplicate. You could have found the answer to this yourself.
Use IDataErrorInfo because then you will be able to validate your input inside ViewModel where you also will be able to access another values/properties/data if needed for your validation.
Futhermore you will be able to swap ViewModels and so change the validation which you will not be able to achieve with ValidationRules easly.
Validation of ViewModel properties should be done in ViewModel. Its centralized and its also testable. It shouldnt not become a part of View except it has to for whatever reason.
You should use ValidationRules when you have binding between two control properties but in this scenario your validation belongs to the "View" anyways and stays inside the View part of MVVM.