如何在 Validation.ErrorTemplate 中找到 ErrorContent
how to find ErrorContent in Validation.ErrorTemplate
我为应用程序中的所有组合框设置了全局样式。对于错误验证,我使用 IDataErrorInfo。因此,当出现错误时,我希望所有文本框都具有这样的自定义视图:
具有不同的 ErrorContent(鼠标应悬停在圆圈上以显示带有错误信息的工具提示)。我的组合框样式是:
<Style x:Key="AlStyleTextBoxArial12" TargetType="{x:Type TextBox}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="12pt"/>
<Setter Property="Margin" Value="3,3,3,3"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Grid DockPanel.Dock="Right" Margin="-23,0,0,0" Width="15" Height="15">
<Grid.ToolTip>
<ToolTip Content="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Grid.ToolTip>
<Ellipse Fill="Red" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
<TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="11pt" FontWeight="Bold" Margin="0,0,0,1"/>
</Grid>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
而我的 TextBox 是这样实现的:
<TextBox Text="{Binding Snr, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
Grid.Row="3" Grid.Column="2"/>
当我有错误时,文本框的视图是可以的,但我无法设置工具提示。它总是空的。为什么我无法获得 ErrorContent 有什么想法吗?
我已经改变了我的风格:
<Style x:Key="AlStyleTextBoxArial12" TargetType="{x:Type TextBox}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="12pt"/>
<Setter Property="Margin" Value="3,3,3,3"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Grid DockPanel.Dock="Right" Margin="-23,0,0,0" Width="15" Height="15">
<Grid.ToolTip>
<ToolTip Content="{Binding [0].ErrorContent}"/>
</Grid.ToolTip>
<Ellipse Fill="Red" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
<TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="11pt" FontWeight="Bold" Margin="0,0,0,1"/>
</Grid>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder x:Name="customAdorner" ToolTip="{Binding [0].ErrorContent}"/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
现在正在运行。
我为应用程序中的所有组合框设置了全局样式。对于错误验证,我使用 IDataErrorInfo。因此,当出现错误时,我希望所有文本框都具有这样的自定义视图:
<Style x:Key="AlStyleTextBoxArial12" TargetType="{x:Type TextBox}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="12pt"/>
<Setter Property="Margin" Value="3,3,3,3"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Grid DockPanel.Dock="Right" Margin="-23,0,0,0" Width="15" Height="15">
<Grid.ToolTip>
<ToolTip Content="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Grid.ToolTip>
<Ellipse Fill="Red" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
<TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="11pt" FontWeight="Bold" Margin="0,0,0,1"/>
</Grid>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
而我的 TextBox 是这样实现的:
<TextBox Text="{Binding Snr, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
Grid.Row="3" Grid.Column="2"/>
当我有错误时,文本框的视图是可以的,但我无法设置工具提示。它总是空的。为什么我无法获得 ErrorContent 有什么想法吗?
我已经改变了我的风格:
<Style x:Key="AlStyleTextBoxArial12" TargetType="{x:Type TextBox}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="12pt"/>
<Setter Property="Margin" Value="3,3,3,3"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Grid DockPanel.Dock="Right" Margin="-23,0,0,0" Width="15" Height="15">
<Grid.ToolTip>
<ToolTip Content="{Binding [0].ErrorContent}"/>
</Grid.ToolTip>
<Ellipse Fill="Red" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
<TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="11pt" FontWeight="Bold" Margin="0,0,0,1"/>
</Grid>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder x:Name="customAdorner" ToolTip="{Binding [0].ErrorContent}"/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
现在正在运行。