C# 未显示Passwordbox Validation.ErrorTemplate
我正在尝试显示密码箱的C# 未显示Passwordbox Validation.ErrorTemplate,c#,wpf,C#,Wpf,我正在尝试显示密码箱的验证.ErrorTemplate。然而,它并没有表现出来。在同一表单上,我有一个用户名文本框,并且错误模板显示正确 datatempalte中密码框的Xaml: <PasswordBox Grid.Row="3" DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType=ContentControl}}"> &
验证.ErrorTemplate
。然而,它并没有表现出来。在同一表单上,我有一个用户名文本框
,并且错误模板
显示正确
datatempalte中密码框的Xaml:
<PasswordBox Grid.Row="3" DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType=ContentControl}}">
<PasswordBox.Style>
<Style>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="14" FontWeight="Bold">*</TextBlock>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</PasswordBox.Style>
<i:Interaction.Behaviors>
<behavior:PasswordBoxBehaviorBinding SPassword="{Binding Path=Password, ValidatesOnNotifyDataErrors=True}" />
</i:Interaction.Behaviors>
</PasswordBox>
问题是,在托管发生验证错误的数据绑定属性的
DependencyObject
上引发了错误。在您的情况下,
意味着您可以从行为中读取错误
在这一点上,我还想建议您不要对SPassword
的绑定进行奇怪的攻击。只需正常设置值:
private void AssociatedObject_PasswordChanged(object sender, System.Windows.RoutedEventArgs e)
{
SPassword = AssociatedObject.SecurePassword;
// use debugger to verify, that the validation errors exist. Otherwise, no need for the following line of code
var behaviorErrors = Validation.GetErrors(this);
}
不幸的是,我还没有找到如何以优雅的方式将附加行为的Validation.Errors
提升到主机控件。因此,基本上,您可以选择以某种方式将错误从行为链接到密码箱,或者创建一个额外的属性绑定,因为此绑定将使用相同的验证机制,从而在密码箱上设置验证.errors
。我决定将viewmodelPassword
绑定到PasswordBox.Tag
以进行错误传播
<PasswordBox Width="200" Height="100" Tag="{Binding Password,ValidatesOnNotifyDataErrors=True,Mode=OneWay}">
<i:Interaction.Behaviors>
<behavior:PasswordBoxBehaviorBinding SPassword="{Binding Password}"/>
</i:Interaction.Behaviors>
</PasswordBox>
否则,请确保适当设置绑定模式。试试@Nathan,谢谢你的建议。然而,这并没有解决这个问题。你尝试过哪些变通方法,结果如何?您是否尝试过在初始化组件后在代码中设置DataContext?您是否尝试过构建一个简单的盗版项目来显示自定义验证错误,并逐步将其修改为当前代码?您是否尝试在Validation.HasError添加触发器以查看是否出现默认验证错误?@Nathan,我一直在尝试使用Visual Studio对其进行调试。我注意到的一件事是用户名的文本框中,如果有错误,则会出现一个新的属性Validation.Errors,其中包含验证错误。密码框没有验证错误的属性。您是否尝试将ValidatesOnDaerRors设置为true?
private void AssociatedObject_PasswordChanged(object sender, System.Windows.RoutedEventArgs e)
{
SPassword = AssociatedObject.SecurePassword;
// use debugger to verify, that the validation errors exist. Otherwise, no need for the following line of code
var behaviorErrors = Validation.GetErrors(this);
}
<PasswordBox Width="200" Height="100" Tag="{Binding Password,ValidatesOnNotifyDataErrors=True,Mode=OneWay}">
<i:Interaction.Behaviors>
<behavior:PasswordBoxBehaviorBinding SPassword="{Binding Password}"/>
</i:Interaction.Behaviors>
</PasswordBox>
public static readonly DependencyProperty PasswordProperty = DependencyProperty.Register(
"SPassword",
typeof(SecureString),
typeof(PasswordBoxBehaviorBinding),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));