简体   繁体   English

如何使用WCF RIA服务在WPF中进行客户端验证

[英]How do I do client-side validation in WPF using WCF RIA Services

I've created a WCF RIA Service that I'd like to use with a WPF application. 我创建了一个WCF RIA服务,该服务想与WPF应用程序一起使用。 I've added several System.ComponentModel.DataAnnotations validation rules on the entities meta-data, all which work great on the server when I call .SubmitChanges(changeSet) from the client. 我已经在实体元数据上添加了几个System.ComponentModel.DataAnnotations验证规则,当我从客户端调用.SubmitChanges(changeSet)时,所有验证规则都可以在服务器上很好地工作。 I'd also like to validate my entities on the client side before I sumbit my changes to the server but I have no idea how to do so. 我还想在对服务器进行更改汇总之前先在客户端上验证我的实体,但我不知道该怎么做。 Any help in this regard would be greatly appreciated! 在这方面的任何帮助将不胜感激! Thanks.... 谢谢....

As far as I know, there is no WCF RIA Services for WPF (although I'd be glad to be proven wrong, as I am waiting for this...), so you have to do the client-side work yourself. 据我所知,没有用于WPF的WCF RIA服务(尽管我很高兴能被证明是错误的,因为我在等待这个……),所以您必须自己进行客户端工作。

Use the VisualTreeHelper to go over every control in your form, and recursively if the control is a panel. 使用VisualTreeHelper可以遍历表单中的每个控件,如果控件是面板,则可以递归地遍历。 For each control, have a list of potentially-bound properties (I guess there is only one in this case). 对于每个控件,都有一个潜在绑定属性的列表(我想在这种情况下只有一个)。 For example, a TextBox will potentially have its TextBoxProperty bound, a CheckBox will have its IsCheckedProperty bound. 例如,一个TextBox可能绑定了TextBoxProperty,而CheckBox则绑定了IsCheckedProperty。 Use BindingOperation.GetBinding to get a Binding instance, which gives you the Source and Path properties. 使用BindingOperation.GetBinding获得Binding实例,该实例为您提供Source和Path属性。 Now use reflection on the source to see if there is a data annotation associated with it. 现在,在源上使用反射来查看是否有与之关联的数据注释。 If there is, check it. 如果有,请检查。

Yes, it is a lot of code. 是的,这是很多代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM