如何使用 WCF RIA 服务在 WPF 中进行客户端验证 [英] How do I do client-side validation in WPF using WCF RIA Services

查看:30
本文介绍了如何使用 WCF RIA 服务在 WPF 中进行客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 WCF RIA 服务,我想将它与 WPF 应用程序一起使用.我在实体元数据上添加了几个 System.ComponentModel.DataAnnotations 验证规则,当我从客户端调用 .SubmitChanges(changeSet) 时,所有这些规则都在服务器上运行良好.我还想在将更改提交到服务器之前在客户端验证我的实体,但我不知道该怎么做.在这方面的任何帮助将不胜感激!谢谢....

I've created a WCF RIA Service that I'd like to use with a WPF application. 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. 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....

推荐答案

据我所知,WPF 没有 WCF RIA 服务(虽然我很高兴被证明是错误的,因为我正在等待这个...),所以你必须自己做客户端工作.

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.

使用 VisualTreeHelper 遍历表单中的每个控件,如果控件是面板,则递归遍历.对于每个控件,都有一个潜在绑定属性的列表(我猜在这种情况下只有一个).例如,一个 TextBox 可能有它的 TextBoxProperty 绑定,一个 CheckBox 可能有它的 IsCheckedProperty 绑定.使用 BindingOperation.GetBinding 获取 Binding 实例,它为您提供 Source 和 Path 属性.现在使用对源的反射来查看是否有与之关联的数据注释.如果有,请检查它.

Use the VisualTreeHelper to go over every control in your form, and recursively if the control is a panel. 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. Use BindingOperation.GetBinding to get a Binding instance, which gives you the Source and Path properties. Now use reflection on the source to see if there is a data annotation associated with it. If there is, check it.

是的,代码很多.

这篇关于如何使用 WCF RIA 服务在 WPF 中进行客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆