How to identify the placement of a ValidationError tooltip in a WPF TextBox

I added an arrow to indicate the TextBox from the ToolTip. This works great when the TextBox is far from Screen Edge. But when it is close to the edge of the screen. Change ToolTip Placement and arrow is displayed on the left.

Here is the image. Correct, as expected, since the TextBox is far from the edges. Correct as expected, since TextBox in away from edges.

But when the TextBox is close to the edges. I see it

But when TextBox is near to edges. I see this

I want to see an arrow in the second image on the right side of the tooltip.

Here is the code

<Grid Grid.Column="0" Width="10" Margin="1,0,-1,0" Background="Transparent"> <Path Height="15" Stretch="Fill" Fill="{DynamicResource ControlsValidationBrush}" Data="F1 M 287.328,237.333L 319.344,255.818L 319.344,218.849L 287.328,237.333 Z " /> </Grid> <Border Grid.Column="1" Background="{DynamicResource ControlsValidationBrush}" CornerRadius="0"> <TextBlock MaxWidth="250" Margin="8,7,8,7" Foreground="{DynamicResource WhiteBrush}" Text="{Binding (Validation.Errors)[0].ErrorContent}" TextWrapping="Wrap" UseLayoutRounding="false" /> </Border> 
+4
source share
1 answer

I created a Controltemplate for the tooltip and showed / hid the left or right arrow depending on the location of the tooltip. Here is Xaml for him:

  <ControlTemplate x:Key="tooltipTemplate" TargetType="{x:Type ToolTip}"> <StackPanel Orientation="Horizontal"> <Grid x:Name="LeftGrid" Width="10" Margin="1,0,-1,0" Background="Transparent"> <Path Height="15" Stretch="Fill" Fill="Red" Data="F1 M 287.328,237.333L 319.344,255.818L 319.344,218.849L 287.328,237.333 Z " /> </Grid> <Border Background="Red" CornerRadius="0"> <TextBlock MaxWidth="250" Margin="8,7,8,7" Foreground="{DynamicResource WhiteBrush}" Text="This is tooltip" TextWrapping="Wrap" UseLayoutRounding="false" /> </Border> <Grid x:Name="RightGrid" Width="10" Margin="1,0,-1,0" Background="Transparent"> <Path Height="15" Stretch="Fill" Fill="Red" Data="F1 M 287.328,237.333L 319.344,255.818L 319.344,218.849L 287.328,237.333 Z " /> </Grid> </StackPanel> <ControlTemplate.Triggers> <Trigger Property="Placement" Value="Left"> <Setter TargetName="LeftGrid" Property="Visibility" Value="Hidden"/> <Setter TargetName="RightGrid" Property="Visibility" Value="Visible"/> </Trigger> <Trigger Property="Placement" Value="Right"> <Setter TargetName="LeftGrid" Property="Visibility" Value="Visible"/> <Setter TargetName="RightGrid" Property="Visibility" Value="Hidden"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> <ToolTip x:Key="myToolTip" Template="{StaticResource tooltipTemplate}"> 

thanks

0
source

Source: https://habr.com/ru/post/1499479/


All Articles