控件模板中的 ValidationRules
我有一个继承自文本框的控件
public class MyTextBox : TextBox
样式
<Style TargetType="{x:Type Controls:MyTextBox}">
,它具有一种 Setter
<Setter Property="Template">
,我希望能够将 Binding.ValidationRules 设置为模板中的某些内容,从而影响此类文本框的所有实例。 因此,我可以制作时间、日期、数字、邮政编码/邮政编码等文本框。或者我想要的任何内容,
我不想每次创建文本框时都必须设置验证规则。我只想说我想要一个 NumericTextBox 并让它以模板中设置的任何方式进行验证。
这可能吗?
到目前为止我所看到的只是在控件的每个实例上设置的 ValidationRules,例如
<TextBox x:Name="txtEMail" Template={StaticResource TextBoxErrorTemplate}>
<TextBox.Text>
<Binding Path="EMail" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<local:RegexValidationRule Pattern="{StaticResource emailRegex}"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
I have control thats inherits from textbox
public class MyTextBox : TextBox
This has a style
<Style TargetType="{x:Type Controls:MyTextBox}">
one of the Setters is
<Setter Property="Template">
I want to be able to set the Binding.ValidationRules
to something in the template, thus affecting all instances of this type of textbox.
I can therefore make textboxes for say Times, Dates, Numerics, post/zip codes.. or whatever i want,
I don't want to have to set the validation rules every time i create a textbox. I just want to say i want a NumericTextBox
and have it validate in whatever way is set in the template.
Is this possible?
All i have seen so far is the ValidationRules being set on each instance of the control e.g.
<TextBox x:Name="txtEMail" Template={StaticResource TextBoxErrorTemplate}>
<TextBox.Text>
<Binding Path="EMail" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<local:RegexValidationRule Pattern="{StaticResource emailRegex}"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如您所见,验证规则是与绑定一起设置的。我遇到了同样的问题,对我来说,可行的解决方案是执行以下操作:
这里的问题是确保在添加验证规则之前设置绑定,因此使用 Loaded,但我不确定如果这适用于所有场景。
As you see, validation rules are set along with bindings. I came across the same problem and the working solution for me was to do something like this:
The problem here is to be sure that the binding is set before we add the validation rule, hence the use of Loaded, but i'm not sure if this will work on every scenario.
我不确定是否可以完全按照您的意愿去做。您可能可以通过属性设置器中的很多技巧来实现这一点(我不会依赖于此,因为它会涉及修改绑定,根据定义,绑定是动态的),或者使用隐藏代码/自定义控件。
相反,我建议您将验证推送到 ViewModel 中,可能使用
IDataErrorInfo
。那里有很多文章。 是我刚刚搜索“MVVM 验证”时发现的第一个:MVVM - 验证
这 将允许您使用标准的面向对象组合技术,因此您可以 避免重复自己 :)
I am not sure it is possible to do exactly what you want. You might be able to do it with a lot of trickery in property setters (I wouldn't rely on this, since it would involve modifying bindings, which are by definition dynamic), or with code behind/custom controls.
Instead I suggest you push your validation into your ViewModel, probably with
IDataErrorInfo
. There are many articles out there. Here's one of the first ones I found with a search just now for "MVVM validation":MVVM - Validation
Doing this will allow you to use standard OO composition techniques, so you can avoid repeating yourself :)