WPF 绑定:使用数据注释进行验证规则
我读过很多关于 WPF 验证和 DataAnnotations
的博客文章。我想知道是否有一种干净的方法来使用 DataAnnotations
作为我的实体的 ValidationRules
。
因此,不要有这个(来源):
<Binding Path="Age" Source="{StaticResource ods}" ... >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
您必须我
public class AgeRangeRule : ValidationRule
{...}
想要 WPF 绑定去查看 Age 属性并查找 DataAnnotation ,有点像这样:
[Range(1, 120)]
public int Age
{
get { return _age; }
set
{
_age = value;
RaisePropertyChanged<...>(x => x.Age);
}
}
如果这可能的话,有什么想法吗?
I have read a lot of Blog post on WPF Validation and on DataAnnotations
. I was wondering if there is a clean way to use DataAnnotations
as ValidationRules
for my entity.
So instead of having this (Source) :
<Binding Path="Age" Source="{StaticResource ods}" ... >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
Where you must have your
public class AgeRangeRule : ValidationRule
{...}
I want the WPF Binding to go see the Age property and look for DataAnnotation a bit like this:
[Range(1, 120)]
public int Age
{
get { return _age; }
set
{
_age = value;
RaisePropertyChanged<...>(x => x.Age);
}
}
Any ideas if this is possible ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我发现的最接近的方法是:
来源
来源和源
这样,DataAnnotation 工作正常,我在 XAML 上要做的事情最少
ValidatesOnDataErrors ="True"
这是 Aaron post 与 DataAnnotation 的一个很好的解决方法。The closest approach I found is :
Source
Source and Source
That way, the DataAnnotation works fine, I got a minimum to do on the XAML
ValidatesOnDataErrors="True"
and it's a fine workaround of Aaron post with the DataAnnotation.在您的模型中,您可以实现 IDataErrorInfo 并执行类似的操作...
您还需要将新定义的行为通知绑定目标。
编辑:
如果您只想使用数据注释,您可以按照此博客文章 概述了如何完成任务。
更新:
上述链接的历史表示。
In your model you could implement
IDataErrorInfo
and do something like this...You will also need to notify the binding target of the newly defined behavior.
EDIT:
If you only want to use Data Annotations you can follow this blog post which outlines how to accomplish the task.
UPDATE:
Historical representation of the aforementioned link.
听起来不错,亚伦。我刚刚进入 WPF,下周将在工作中学习数据绑定;)所以无法完全判断你的答案...
但是对于 winforms,我使用了 来自 Entlib 的验证应用程序块并实现了 IDataErrorInfo(实际上IDXDataErrorInfo 因为我们在基本实体(业务对象)上使用 DevExpress 控件),效果非常好!
它比您所描绘的解决方案要复杂一些,您将验证逻辑放置在对象上而不是接口实现中。使其更加面向对象且可维护。在 ID(XD)ataErrorInfo 处,我只需调用 Validation.Validate(this),或者更好地获取调用接口的属性的验证器并验证特定的验证器。不要忘记调用 [SelfValidation],因为要验证属性组合;)
Sounds good Aaron. I'm just into WPF and will study databindings next week at work ;) So cannot completely judge your answer...
But with winforms I have used Validation Application Block from the Entlib and implemented IDataErrorInfo (actually IDXDataErrorInfo because we work with DevExpress controls) on a base entity (business object) and that works pretty fine!
It's a bit more sophisticated than the solution you sketched in this way that you place your validation logic on the object and not in the interface implementation. Making it more OOP and maintainable. At the ID(XD)ataErrorInfo I just call Validation.Validate(this), or even better get the validator for the property that the interface is called for and validate the specific validator. Don't forget to call the [SelfValidation] as well because of validation for combinations of properties ;)
您可能对WPF 应用程序框架 (WAF)的 BookLibrary 示例应用程序感兴趣a>。它使用 DataAnnotations Validation 属性和 WPF Binding。
You might be interested in the BookLibrary sample application of the WPF Application Framework (WAF). It uses the DataAnnotations Validation attributes together with WPF Binding.
最近,我有同样的想法,使用数据注释 API 来验证 WPF 中的 EF Code First POCO 类。就像 Philippe 的帖子一样,我的解决方案使用反射,但所有必要的代码都包含在通用验证器中。
上面的代码显示了一个从通用 GenericValidationRule 类派生的 ClientValidatorRule。 Client 类是我的 POCO 类,将对其进行验证。
Recently I've had the same idea using the Data Annotation API to validate EF Code First POCO classes in WPF. Like Philippe's post my solution uses reflection, but all necessary code is included in a generic validator.
The code above shows a ClientValidatorRule which is derived from the generic GenericValidationRule class. The Client class is my POCO class which will be validated.