在键盘焦点和鼠标悬停时在工具提示中显示 IDataErrorInfo 错误
Show IDataErrorInfo errors in tooltip on keyboard focus and mouse over
我有一个 TextBox
,它使用这种样式通过 ToolTip
显示 IDataErrorInfo
验证信息:
<Style x:Key="EntityPropertyTextBoxErrorStyle" TargetType="{x:Type TextBox}"
BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<!-- this gets rid of all adornment INCLUDING THE DEFAULT RED BORDER -->
<AdornedElementPlaceholder />
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
<Setter Property="Background" Value="MistyRose" />
</Trigger>
</Style.Triggers>
</Style>
现在我希望 ToolTip
也显示在键盘焦点上。
我找到的最佳解决方案是使用 Popup
而不是 ToolTip
:
<ControlTemplate x:Key="ErrorTemplate">
<StackPanel>
<AdornedElementPlaceholder x:Name="ControlWithError" />
<Popup PlacementTarget="{Binding ElementName=ControlWithError}" Placement="Top">
<Popup.IsOpen>
<MultiBinding Converter="{StaticResource AtLeastOneTrueConverter}">
<Binding Path="AdornedElement.IsMouseOver"
ElementName="ControlWithError" Mode="OneWay" />
<Binding Path="AdornedElement.IsKeyboardFocusWithin"
ElementName="ControlWithError" Mode="OneWay" />
</MultiBinding>
</Popup.IsOpen>
<Border BorderThickness="1">
<TextBlock Text="{Binding
AdornedElement.(Validation.Errors)[0].ErrorContent,
ElementName=ControlWithError}"
Background="White"
FontSize="8" />
</Border>
</Popup>
</StackPanel>
</ControlTemplate>
<Style x:Key="EntityPropertyTextBoxErrorStyle" TargetType="{x:Type TextBox}"
BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorTemplate}" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="MistyRose" />
</Trigger>
</Style.Triggers>
</Style>
我有一个 TextBox
,它使用这种样式通过 ToolTip
显示 IDataErrorInfo
验证信息:
<Style x:Key="EntityPropertyTextBoxErrorStyle" TargetType="{x:Type TextBox}"
BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<!-- this gets rid of all adornment INCLUDING THE DEFAULT RED BORDER -->
<AdornedElementPlaceholder />
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
<Setter Property="Background" Value="MistyRose" />
</Trigger>
</Style.Triggers>
</Style>
现在我希望 ToolTip
也显示在键盘焦点上。
我找到的最佳解决方案是使用 Popup
而不是 ToolTip
:
<ControlTemplate x:Key="ErrorTemplate">
<StackPanel>
<AdornedElementPlaceholder x:Name="ControlWithError" />
<Popup PlacementTarget="{Binding ElementName=ControlWithError}" Placement="Top">
<Popup.IsOpen>
<MultiBinding Converter="{StaticResource AtLeastOneTrueConverter}">
<Binding Path="AdornedElement.IsMouseOver"
ElementName="ControlWithError" Mode="OneWay" />
<Binding Path="AdornedElement.IsKeyboardFocusWithin"
ElementName="ControlWithError" Mode="OneWay" />
</MultiBinding>
</Popup.IsOpen>
<Border BorderThickness="1">
<TextBlock Text="{Binding
AdornedElement.(Validation.Errors)[0].ErrorContent,
ElementName=ControlWithError}"
Background="White"
FontSize="8" />
</Border>
</Popup>
</StackPanel>
</ControlTemplate>
<Style x:Key="EntityPropertyTextBoxErrorStyle" TargetType="{x:Type TextBox}"
BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorTemplate}" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="MistyRose" />
</Trigger>
</Style.Triggers>
</Style>