在 WPF 中验证密码框

ivi*_*oke 6 c# wpf datatemplate

有没有办法在验证 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>
Run Code Online (Sandbox Code Playgroud)

和我的 BaseControlStyle 即时通讯使用该验证

<Style TargetType="Control"
           x:Key="ControlBaseStyle">
        <Setter Property="Validation.ErrorTemplate"
                Value="{StaticResource DefaultErrorTemplate}" />
Run Code Online (Sandbox Code Playgroud)

它对我拥有的几乎所有控件(Combobox、DateTimePicker、TextBox)都非常有用,但是当我想使用相同的样式时,passwordBox它就不起作用了。

在此处输入图片说明 在图片中,您可以看到它与 simpe TextBox 一起工作,但不与 PasswordBox 一起工作。我不知道如何提取错误消息以在工具提示中显示它AdornedElementPlaceholder

它显示用户名属性的错误消息

[Required(ErrorMessage = "Please enter username.")]
Run Code Online (Sandbox Code Playgroud)

我不想用 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); }
}
Run Code Online (Sandbox Code Playgroud)

并使用 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" />
Run Code Online (Sandbox Code Playgroud)

并制作了自定义的 PasswordBoxStyle BasedOn ControlBaseStyle

<Style TargetType="{x:Type PasswordBox}"
       BasedOn="{StaticResource ControlBaseStyle}">
Run Code Online (Sandbox Code Playgroud)

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

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

ivi*_*oke 1

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

<Style TargetType="Control"
       x:Key="ControlBaseStyle">
    <Setter Property="Validation.ErrorTemplate"
            Value="{StaticResource DefaultErrorTemplate}" />
Run Code Online (Sandbox Code Playgroud)

并把它放进去

<Trigger Property="Validation.HasError"
         Value="True">
    <Setter Property="Validation.ErrorTemplate"
            Value="{StaticResource DefaultErrorTemplate}" />
Run Code Online (Sandbox Code Playgroud)

显然,它仅在触发器内有效,并且 TextBox 可以像默认值一样在触发器内工作。