English 中文(简体)
MVVM 探测器校验。
原标题:MVVM detect Validation.HasError in View Model
  • 时间:2012-05-22 09:45:54
  •  标签:
  • wpf

我使用 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 代码中检查它们是否无效。 如果有更好的办法, 我宁愿不要这样做 。

最佳回答

为何不使用字符串属性而不在查看模型中使用 IDataErrorInfo 验证? 在您的保存命令中, 如果您的 IDataErrorInfo 没有错误, 您可以安全地将字符串转换为 int 值 。 使用 IDataErrorInfo 的字符串属性是最容易的方法 。

编辑: 再想想, 如果您不使用字符串属性, 还有一个问题。 假设您有一个 int 属性, 用户在文本框中设置了 10 。 所以在您的视图模型中, 您现在有 10 。 现在用户删除 10 并在文本框中设置 abc 。 您的视图模型仍然有 10 。 因为有绑定的连接例外。 这就是为什么我几乎要使用字符串属性。 公平地说, 您可以使用文本框的行为来设置遮罩, 这样用户无法输入无效的数据 。

问题回答

我可以马上想到两个强有力的选项。 一个是连接到您 ViewModel 中的字符串属性, 而该属性又被编程到只解析并在字符串被确定为有效时存储内在的整数值。 这确保了您的 TextBox 总是能够成功存储它的数据值 。

第二是拦截在您视图中出现的校验例外, 通过习惯行为将其存储在您的视图模式中 。 < a href=" http://www.codeproject.com/ artists/97564/Attributes-based- validation- in- a-WPF-MVM-Applicat" rel="nofollow" > 这篇文章 将基本上照您在问题中所描述的做 。

您可以尝试的是 BindingGroups , 并对整个元素进行验证, 而不仅仅是单个属性。 我用它来为我们的模式对话框创建一个工程, 例如, 在完成对话框之前必须设置某些设置 。 此 < href=" http://blogs. msdn. com/b/ vinsibal/ archive/2008/08/11/ wpf-3-5- sp1- fatural- contical groups- with - ject- level- validation. aspx" rel= "no follow" >link 详细解释了它。 < a href=" < http://msdn. microcyft.com/ en-us/magazine/ff714593. aspx" rel=“ nofolpolth > > this a > a > a is 也非常详细地解释 。





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签