在 WPF 中验证密码框
Validate PasswordBox in WPF
有没有办法在验证 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
使用相同的样式时,它不起作用。
在图片中,您可以看到它与 simpe TextBox 一起工作,但不能与 PasswordBox 一起工作。我不知道如何提取错误消息以在 AdornedElementPlaceholder
的工具提示中显示
显示用户名 属性
的错误消息
[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" />
并制作了 BasedOn ControlBaseStyle
的自定义 PasswordBoxStyle
<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}" />
显然它只有在 Trigger 内部才能工作,而 TextBox 可以像默认情况一样在 Trigger 内部工作。
有没有办法在验证 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
使用相同的样式时,它不起作用。
AdornedElementPlaceholder
显示用户名 属性
的错误消息[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" />
并制作了 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}" />
显然它只有在 Trigger 内部才能工作,而 TextBox 可以像默认情况一样在 Trigger 内部工作。