在验证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
它显示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" />
并制作了BasedOn ControlBaseStyle
<Style TargetType="{x:Type PasswordBox}"
BasedOn="{StaticResource ControlBaseStyle}">
我对TextBox
做了同样的事情,但它与PasswordBox
不起作用。
INFO:我想知道你为什么投票结束这个问题?如果有这个甚至是指南的答案,我很乐意自己关闭,如果没有,请在投票结束之前给我一个答案。感谢。
答案 0 :(得分:0)
我找到了答案。 不得不改变
<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中工作。