加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在WPF中验证PasswordBox

发布时间:2020-12-15 21:07:02 所属栏目:百科 来源:网络整理
导读:有没有办法在验证PasswordBox时在AdornedElementPlaceholder中显示错误消息. 我有这样的事情: ControlTemplate x:Key="DefaultErrorTemplate" StackPanel Orientation="Horizontal" AdornedElementPlaceholder x:Name="placeholder" / Border Background="R
有没有办法在验证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一样工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读