将 ValidationRules 添加到单个 xaml 行或简写 ValidationRules
我正在使用一个密码框,它公开了一个依赖属性,以便我可以绑定到它。问题是,通过像这样使用它,我无法将 Binding.ValidationRules 速记为以下语法:
<PasswordBox services:RPLPasswordBoxBinder.BindPassword="True"
services:RPLPasswordBoxBinder.BoundPassword="{Binding Path=LoginUser.Parola, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</PasswordBox>
我将 ValidationRules 设置为如下文本框:
<TextBox.Text>
<Binding Path="LoginUser.Parola" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<some validation rule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
有没有办法在单个 xaml 行中将 ValidationRules 集合指定到我的 PasswordBox?或者也许还有另一个聪明的解决方案来验证用户在密码框中的输入?
一些说明:
我正在使用 MVVM,我不想使用后台代码。
我只想添加一个 ValidationRule。也许速记 Binding.ValidationRules 的问题在于该属性是一个集合。在我的情况下,一条验证规则就足够了。
stackoverflow 上有一个类似的问题此处。我的问题有所不同,因为我不仅想提高可读性,还想实际验证我的密码框。
I'm using a PasswordBox which exposes a dependency property such that I can bind to it. The problem is that by using it like so, I cannot shorthand the Binding.ValidationRules to this syntax:
<PasswordBox services:RPLPasswordBoxBinder.BindPassword="True"
services:RPLPasswordBoxBinder.BoundPassword="{Binding Path=LoginUser.Parola, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</PasswordBox>
I set my ValidationRules to a textbox like this:
<TextBox.Text>
<Binding Path="LoginUser.Parola" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<some validation rule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
Is there any way to specify the ValidationRules collection to my PasswordBox in a single xaml line? Or maybe there's another clever solution for validating user input into my password box?
Some clarifications:
I'm using MVVM and I don't want to use code behind.
I want to add only a single ValidationRule. Maybe the problem with shorthanding Binding.ValidationRules is that this property is a collection. One validationrule would suffice in my situation.
There's a similar question on stackoverflow here. My problem is different as I don't just want to increase readability but actually validate my PasswordBox.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您将数据模型类基于 IDataErrorInfo,然后在此处执行验证,而不是在后面的代码中执行验证。
有很多示例,但是 这里是一个适合初学者,另一个此处。
I suggest that you base your data model class on IDataErrorInfo and then validation is performed there and not in the code behind.
There are plenty of examples, but here's one for starters and another here.