有没有办法在验证 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
它不起作用。
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:我想知道您为什么投票结束这个问题?如果对此有答案甚至指南,我很乐意自己关闭它,如果没有,请在投票关闭它之前给我一个答案。谢谢。