我使用 MVVM, 并使用 IDataErrorInfo 和 我的 ViewModel 完成大部分验证, 并使用 IDataErrorInfo 和 我的 ViewModel 拥有一个 伊斯瓦利德 属性, 该属性检查每个成员的有效性, 需要验证 。 然而, 我有一些无法无效的文本框, 所以我使用 校验规则 提醒用户( 使用比 “ 价值无法转换” 更友好的信息 ), 如果他们将域空白, 因为显然 属性设置者从来没有被调用过 IDataErrorInfo 代码, 所以没有被调用 IDataErrinfo 代码 。
问题是我有一个“ 保存” 按钮( 即中继Command), 如果有任何校验错误, 我要禁用该按钮 。 因此命令的 CanExercute 检查 VM s IsValid 属性。 但是, 显然, 如果用户把我的内野空了, IDataErrorInfo 对它一无所知, 目前该按钮不会被禁用 。 是否有一种方法可以让 ViewModel 检测出错误?
I thought I d found a solution here http://wpfglue.wordpress.com/2009/12/03/forwarding-the-result-of-wpf-validation-in-mvvm/ but having translated it to C# I can t get it working (the Coerce callback is never called). I don t understand dependency properties and objects very well yet (very new to WPF) and this solution looks complicated to me.
我唯一能想到的就是摆脱验证规则, 制造一个无效的内装包装器, 把目标NullValue= 放在装订器中, 然后在 IDataErrInfo 代码中检查它们是否无效。 如果有更好的办法, 我宁愿不要这样做 。