【问题标题】:Add ValidationRules into a single xaml line OR shorthand ValidationRules将 ValidationRules 添加到单个 xaml 行或简写 ValidationRules
【发布时间】:2011-12-14 00:01:35
【问题描述】:

我正在使用一个 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 行中为我的 PasswordBox 指定 ValidationRules 集合?或者也许还有另一个聪明的解决方案来验证用户输入到我的密码框?

一些说明:

我正在使用 MVVM,我不想使用后面的代码。

我只想添加一个 ValidationRule。也许简写 Binding.ValidationRules 的问题在于该属性是一个集合。在我的情况下,一个验证规则就足够了。

stackoverflow here 上有一个类似的问题。我的问题有所不同,因为我不仅想提高可读性,还想验证我的 PasswordBox。

【问题讨论】:

  • 这最初是在大约十年前被问到的,而 Florin 自 2016 年以来就没有出现过。也许在新答案中对当时和现在的代码之间的差异进行新的答案和解释会更好。跨度>

标签: wpf binding shorthand validationrules


【解决方案1】:

我建议您将数据模型类基于 IDataErrorInfo,然后在那里执行验证,而不是在后面的代码中。

有很多例子,但here's one for starters 和另一个here

【讨论】:

  • 工作就像一个魅力。不过,对于初学者,我建议以this msdn link 为例。我发现它更易于阅读和复制。
猜你喜欢
  • 2017-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
相关资源
最近更新 更多