我使用了一个PasswordBox,它公开了一个依赖属性,这样我就可以绑定到它。问题是,通过这样使用它,我无法将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 here上也有类似的问题。我的问题是不同的,因为我不仅想增加可读性,还想实际验证我的PasswordBox。
发布于 2011-10-24 18:20:17
我建议您将数据模型类基于IDataErrorInfo,然后在那里执行验证,而不是在后面的代码中执行验证。
有很多例子,但是here's one for starters和另一个here。
https://stackoverflow.com/questions/7873821
复制相似问题