如何进行PasswordBox验证?

时间:2015-08-20 01:43:44

标签: wpf validation mvvm passwordbox

关于如何将PasswordBoxViewModel绑定,我关注this tutorial,但我想在PasswordBox上添加一些验证,就像TextBox一样:

ValidationError

我怎样才能实现这一点,仍然使用此方法进行PasswordBox绑定?

1 个答案:

答案 0 :(得分:0)

您可以为PasswordBoxTriggers设置样式我们可以更新Style

<PasswordBox>
  <PasswordBox.Style>
    <Style TargetType="{x:Type PasswordBox}">
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding IsFocused,RelativeSource={RelativeSource Self}}"
                               Value="False" />
                    <Condition Binding="{Binding IsError}"
                               Value="True" />
                </MultiDataTrigger.Conditions>
                <Setter Property="BorderBrush"
                        Value="Red" />
            </MultiDataTrigger>
        </Style.Triggers>
     </Style>
  </PasswordBox.Style>
</PasswordBox>

在此代码中IsError属性将是您的ViewModel属性要查找天气,此密码有任何错误。然后更新BorderBrush的{​​{1}}。