我有一个带有验证的控件和一个最初不可见的错误模板设置。当ContentControl绑定的属性切换到它时,控件变为可见。但是,当控件可见时,错误模板仅在更新绑定属性后应用。有什么想法可能会发生这种情况以及我能做些什么呢?
XAML控件摘录:
<TextBox Name="UserNameTextBox" Grid.Row="0" Grid.Column="1" Style="{StaticResource WinformsErrorTemplate}" Text="{Binding Path=UserName, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" IsEnabled="{Binding Path=CanEditCredentials}"/>
XAML错误模板:
<!-- This error template style emulates a Winforms validation error icon -->
<Style x:Key="WinformsErrorTemplate" TargetType="Control">
<Setter Property="Margin" Value="3"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Ellipse DockPanel.Dock="Right"
ToolTip="{Binding ElementName=myTextbox,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
Width="15" Height="15"
Margin="-25,0,0,10"
StrokeThickness="1" Fill="Red" >
<Ellipse.Stroke>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFFA0404" Offset="0"/>
<GradientStop Color="#FFC9C7C7" Offset="1"/>
</LinearGradientBrush>
</Ellipse.Stroke>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource FlashErrorIcon}"/>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
<TextBlock DockPanel.Dock="Right"
ToolTip="{Binding ElementName=myControl,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
Foreground="White"
FontSize="11pt"
Margin="-15,0,0,5" FontWeight="Bold">!
<TextBlock.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource FlashErrorIcon}"/>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
<Border BorderBrush="Red" BorderThickness="1" Margin="0,0,0,10">
<AdornedElementPlaceholder Name="myControl"/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
答案 0 :(得分:2)
找到解决方案。问题是Window级别有一个AdornerDecorator,但没有任何UserControls。因此,ErrorTemplate无法呈现。解决方法是在AdornerDecorator中封装UserControl下的所有内容:
<UserControl>
<AdornerDecorator>
<StackPanel>
...
</StackPanel>
<AdornerDecorator>
</UserControl>
有关详细信息,请参阅Validation ErrorTemplate not showing on data errors。
答案 1 :(得分:0)
对于在制表符控件中有多个视图的程序,我遇到了同样的问题。 将显示验证错误,但当我切换到另一个选项卡并再次返回时,它将消失。 将AdornerDecorator添加到每个视图可解决问题。 感谢