How to identify the ValidationError ToolTip Placement in WPF TextBox

只谈情不闲聊 提交于 2020-01-02 06:25:12

问题


I have added a Arrow to Indicate the TextBox from ToolTip. This Works great when the TextBox is far away from the Screen Edge. But When it is near the Screen edge. The ToolTip Placement changes and the Arrow is shown on Left.

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

But when TextBox is near to edges. I see this

I want to see the Arrow in the second image on the right side of 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>

回答1:


I created the Controltemplate for the tooltip and show/hide right or left arrow depending on the placement of the tooltip. Here is the Xaml for it:

       <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



来源:https://stackoverflow.com/questions/18497029/how-to-identify-the-validationerror-tooltip-placement-in-wpf-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!