wpf - 将 ValidationRules 添加到单个 xaml 行或简写 ValidationRules

标签 wpf binding shorthand validationrules

我正在使用一个 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/

相关文章:

c# - 如何在 WPF ViewModel 中使用模型验证规则

c# - 简单的 WPF 示例会导致内存增长失控

c# - Web 内容在 ChromiumFX 中呈现两次

c++ - 有人可以解释速记赋值运算符的实际工作原理吗?

c# - 使用 RadialGradientBrush 绘制球体

像 xaml 一样的 html 元素绑定(bind)

带有格式的 WPF 文本框绑定(bind)

WPF 绑定(bind)到代理

css - 是否有定位的简写属性?

html - CSS 边框规则的简写