首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ValidationRules添加到单个xaml行或速记ValidationRules中

将ValidationRules添加到单个xaml行或速记ValidationRules中
EN

Stack Overflow用户
提问于 2011-10-24 17:50:45
回答 1查看 2.8K关注 0票数 4

我使用了一个PasswordBox,它公开了一个依赖属性,这样我就可以绑定到它。问题是,通过这样使用它,我无法将Binding.ValidationRules简写为以下语法:

代码语言:javascript
复制
<PasswordBox services:RPLPasswordBoxBinder.BindPassword="True" 
             services:RPLPasswordBoxBinder.BoundPassword="{Binding Path=LoginUser.Parola, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
</PasswordBox>

我将ValidationRules设置为一个文本框,如下所示:

代码语言:javascript
复制
<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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-24 18:20:17

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

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

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7873821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档