如何识别 WPF TextBox 中的 ValidationError ToolTip Placement [英] How to identify the ValidationError ToolTip Placement in WPF TextBox

查看:19
本文介绍了如何识别 WPF TextBox 中的 ValidationError ToolTip Placement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个箭头来指示工具提示中的文本框.当 TextBox 远离屏幕边缘时,这很有效.但是当它靠近屏幕边缘时.工具提示位置发生变化,箭头显示在左侧.

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.

这是图片按预期正确,因为 TextBox 远离边缘.

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

但是当 TextBox 靠近边缘时.我看到这个

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.

这是代码

<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>

推荐答案

我为工具提示创建了 Controltemplate 并根据工具提示的位置显示/隐藏向右或向左箭头.这是它的 Xaml:

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}">

谢谢

这篇关于如何识别 WPF TextBox 中的 ValidationError ToolTip Placement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆