在 WPF 中验证密码框

2024-04-04

有没有办法在验证 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 At picture you can see that it's working with simpe TextBox but not with PasswordBox. I don't know how to extract error message to show it in tooltip i that AdornedElementPlaceholder

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

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

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

非常感谢任何帮助。

提前致谢 :)

EDIT:

我已将其用于密码属性

[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.

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


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

<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}" />

显然它只在触发器内部工作,并且文本框可以像默认值一样工作并且在触发器内部工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 WPF 中验证密码框 的相关文章

随机推荐