我正在使用一个 PasswordBox,它公开了一个依赖属性,以便我可以绑定(bind)到它。问题是,通过这样使用它,我无法将 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 行中为我的 PasswordBox 指定 ValidationRules 集合?或者也许有另一个聪明的解决方案来验证用户输入到我的密码框?
一些澄清:
我正在使用 MVVM,我不想使用后面的代码。
我只想添加一个 ValidationRule。也许简写 Binding.ValidationRules 的问题在于该属性是一个集合。在我的情况下,一个验证规则就足够了。
stackoverflow 上有一个类似的问题 here .我的问题有所不同,因为我不仅想提高可读性,还想验证我的 PasswordBox。
最佳答案
我建议您将数据模型类基于 IDataErrorInfo,然后在此处而不是在后面的代码中执行验证。
有很多例子,但是here's one for starters和另一个 here .
关于wpf - 将 ValidationRules 添加到单个 xaml 行或简写 ValidationRules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7873821/