微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 在WPF中验证PasswordBox

有没有办法在验证PasswordBox时在AdornedElementPlaceholder中显示错误消息.

我有这样的事情:

<ControlTemplate x:Key="DefaultErrorTemplate">
    <StackPanel Orientation="Horizontal">
        <AdornedElementPlaceholder x:Name="placeholder" />
        <Border Background="Red"
                ToolTip="{Binding ElementName=placeholder,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                ToolTipService.InitialShowDelay="0"
                VerticalAlignment="Top"
                Margin="3"
                Width="20"
                Height="20"
                CornerRadius="10">
            <TextBlock Text="!"
                       Foreground="White"
                       FontWeight="Bold"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center" />
        </Border>
    </StackPanel>
</ControlTemplate>

和我的BaseControlStyle我使用该验证

<Style targettype="Control"
           x:Key="ControlBaseStyle">
        <Setter Property="Validation.ErrorTemplate"
                Value="{StaticResource DefaultErrorTemplate}" />

它的工作就像一个几乎所有控件(ComboBox,DateTimePicker,TextBox)的魅力,但是当我想为passwordBox使用相同的样式时,它不起作用.

enter image description here


图片中,您可以看到它与simpe TextBox一起使用,但不与PasswordBox一起使用.我不知道如何提取错误消息以在AdtinedElementPlaceholder的工具提示显示

显示Username属性错误消息

[required(ErrorMessage = "Please enter username.")]

我不想用passwordBox实现同样的功能,以便在输入密码时向用户提供有关错误(约束)的反馈

任何帮助都非常感谢.

提前致谢 :)

编辑:

我用它来做密码属性

[required(ErrorMessage = "Please enter password.")]
[RegularExpression(@"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).*$")]
[StringLength(maximumLength: 15,ErrorMessage = "Minimum 8 and maximum 15 characters.",MinimumLength = 8)]
public string Password
{
    get { return GetValue<string>(); }
    set { SetValue(value); }
}

并使用PasswordBoxAssistant绑定到该属性

<PasswordBox behaviors:PasswordBoxAssistant.BindPassword="True"
             behaviors:PasswordBoxAssistant.BoundPassword="{Binding Player.Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
             FontSize="{StaticResource defaultFontSize}"
             Grid.Row="2"
             Grid.Column="1"
             Margin="10" />

并使自定义PasswordBoxStyle为BasedOn ControlBaseStyle

<Style targettype="{x:Type PasswordBox}"
       BasedOn="{StaticResource ControlBaseStyle}">

我用TextBox做了同样的事情,但它不能用于PasswordBox.

信息:我想知道为什么你投票结束这个问题?如果有这个甚至是指南的答案,我很乐意自己关闭,如果没有,请在投票结束之前给我一个答案.谢谢.

解决方法

我找到了答案.
不得不改变

<Style targettype="Control"
       x:Key="ControlBaseStyle">
    <Setter Property="Validation.ErrorTemplate"
            Value="{StaticResource DefaultErrorTemplate}" />

把它放进去

<Trigger Property="Validation.HasError"
         Value="True">
    <Setter Property="Validation.ErrorTemplate"
            Value="{StaticResource DefaultErrorTemplate}" />

显然它只在Trigger内部工作时,TextBox可以像认和Trigger一样工作.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐