wpf 以编程方式显示工具提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23673881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:44:00  来源:igfitidea点击:

Programmatically showing a ToolTip

wpftooltip

提问by user1139666

I have developed a WPF sample project.

我开发了一个 WPF 示例项目。

Here is the main Window's XAML markup :

这是主窗口的 XAML 标记:

<Window x:Class="ToolTipSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        WindowState="Maximized">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Button Click="OnButtonClick">Show ToolTip</Button>

        <StatusBar Grid.Row="2">
            <StatusBarItem>
                <TextBlock Text="TextBlock With ToolTip">
                    <TextBlock.ToolTip>
                        <ToolTip x:Name="m_toolTip">
                            ToolTip
                        </ToolTip>
                    </TextBlock.ToolTip>
                </TextBlock>
            </StatusBarItem>
        </StatusBar>
    </Grid>
</Window>

Here is the main Window's code-behind without the using statements :

这是没有 using 语句的主窗口的代码隐藏:

namespace ToolTipSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnButtonClick(object p_sender, RoutedEventArgs p_args)
        {
            m_toolTip.IsOpen = true;
        }
    }
}

I want to programmatically show the ToolTip when the Button is clicked.
I want the ToolTip to be shown above its TextBlock parent.

我想在单击按钮时以编程方式显示工具提示。
我希望 ToolTip 显示在其 TextBlock 父级上方。

The ToolTip is automatically shown when the mouse cursor is over the TextBlock and during a constant amount of time C approximatively equal to 5 seconds.
I want the ToolTip to be shown during C when the Button is clicked.

当鼠标光标在 TextBlock 上并且在大约等于 5 秒的恒定时间 C 期间,将自动显示工具提示。
我希望在单击按钮时在 C 期间显示工具提示。

My goals are not achieved in the current project.
The ToolTip is shown when the Button is clicked :

我的目标在当前项目中没有实现。
单击按钮时会显示工具提示:

enter image description here

在此处输入图片说明

But :

但 :

  • The ToolTip is too far from its TextBlock parent.
  • The ToolTip is not automatically hidden
  • 工具提示与其 TextBlock 父级相距太远。
  • 工具提示不会自动隐藏

What do I have to do to achieve my goals ?
Any help will be greatly appreciated.

我必须做什么才能实现我的目标?
任何帮助将不胜感激。

采纳答案by user1139666

I have achieved my goals.
Sheridan's answer and comments help me.
I have updated my sample project.
The ToolTip is now shown just above the TextBlock when the Button is clicked.
And a Timer's callback method closes the ToolTip after a constant amount of time equal to 2500 ms.

我已经实现了我的目标。
Sheridan 的回答和评论对我有帮助。
我已经更新了我的示例项目。
当单击按钮时,工具提示现在显示在 TextBlock 的正上方。
定时器的回调方法在等于 2500 毫秒的恒定时间后关闭工具提示。

Here is the updated main Window's XAML markup :

这是更新后的主窗口的 XAML 标记:

<Window x:Class="ToolTipSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        WindowState="Maximized">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Button Click="OnButtonClick">Show ToolTip</Button>

        <StatusBar Grid.Row="2">
            <StatusBarItem>
                <TextBlock Width="900" />
            </StatusBarItem>
            <StatusBarItem>
                <TextBlock x:Name="m_statusMessage" Text="TextBlock With ToolTip" ToolTipService.ShowDuration="30000">
                    <TextBlock.ToolTip>
                        <ToolTip x:Name="m_toolTip" Placement="Top">
                            <TextBlock>
                                ToolTip ToolTipToolTipToolTipToolTipToo lTipToolTipToolTipT oolTipToolTipT
                                <LineBreak />
                                oolTipToolTi pToolTipToo lTipToolTipToolTipToolTipToolTipTo olTipToolTipToolTipTool
                                <LineBreak />
                                TipToo lTipToolTipToolTipToo lTipToolTipTo olTipToolTip
                            </TextBlock>
                        </ToolTip>
                    </TextBlock.ToolTip>
                </TextBlock>
            </StatusBarItem>
        </StatusBar>
    </Grid>
</Window>

Here is the updated main Window's code-behind without the using statements :

这是更新后的主窗口的代码隐藏,没有 using 语句:

namespace ToolTipSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Timer m_toolTipClosingTimer;

        public MainWindow()
        {
            InitializeComponent();
            m_toolTipClosingTimer = new Timer(ToolTipClosingCallBack, null, Timeout.Infinite, Timeout.Infinite);
        }

        private void OnButtonClick(object p_sender, RoutedEventArgs p_args)
        {
            m_toolTip.PlacementTarget = m_statusMessage;
            m_toolTip.IsOpen = true;
            m_toolTipClosingTimer.Change(2500, Timeout.Infinite);
        }

        private void ToolTipClosingCallBack(object p_useless)
        {
            Dispatcher.Invoke(() =>
                {
                    m_toolTip.IsOpen = false;
                });
        }
    }
}

回答by Sheridan

You have a number of problems. The first and simplest to fix is that you are only opening and not closing the ToolTip. You said I want the ToolTip to be shown during the same amount of time when the Button is clicked, and this is easily implemented handling the PreviewMouseDownand PreviewMouseUpevents:

你有很多问题。第一个也是最简单的解决方法是您只打开而不是关闭ToolTip. 你说我希望 ToolTip 在 Button 被点击的相同时间内显示,这很容易实现处理PreviewMouseDownPreviewMouseUp事件:

private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    m_toolTip.PlacementTarget = PlacementTarget;
    m_toolTip.IsOpen = true;
}

private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
    m_toolTip.IsOpen = false;
}

...

...

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Button PreviewMouseDown="Button_PreviewMouseDown" 
        PreviewMouseUp="Button_PreviewMouseUp">Show ToolTip</Button>
    <StatusBar Grid.Row="2">
        <StatusBarItem>
            <TextBlock Name="PlacementTarget" Text="TextBlock With ToolTip">
                <TextBlock.ToolTip>
                    <ToolTip x:Name="m_toolTip" Placement="Top" HorizontalOffset="50" 
                        VerticalOffset="-5">ToolTip</ToolTip>
                </TextBlock.ToolTip>
            </TextBlock>
        </StatusBarItem>
    </StatusBar>
</Grid>

Your other problem is slightly more tricky to fix... it seems to me as though there may be some bug involved in the positioning of your ToolTip. Normally, despite what @icebat said, it ispossible to alter the position of a ToolTipusing the ToolTip.Placementproperty. This can be set to one of the PlacementModeEnumerations.

你的另一个问题有点难以解决……在我看来,你的ToolTip. 通常情况下,尽管什么@icebat说,这可以改变的位置ToolTip使用的ToolTip.Placement属性。这可以设置为PlacementMode枚举之一

The default value is Mouseand this is the definition from the linked page on MSDN:

默认值是Mouse,这是 MSDN 上链接页面的定义:

A postion of the Popup control that aligns its upper edge with the lower edge of the bounding box of the mouse and aligns its left edge with the left edge of the bounding box of the mouse. If the lower screen-edge obscures the Popup, it repositions itself to align with the upper edge of the bounding box of the mouse. If the upper screen-edge obscures the Popup, the control repositions itself to align with the upper screen-edge.

Popup 控件的一个位置,将其上边缘与鼠标边界框的下边缘对齐,并将其左边缘与鼠标边界框的左边缘对齐。如果下屏幕边缘遮挡了 Popup,它会重新定位自身以与鼠标边界框的上边缘对齐。如果上屏幕边缘遮挡了 Popup,则控件会重新定位自身以与上屏幕边缘对齐。

This explains why the ToolTipis displayed far from the TextBlockplacement target... because the Buttonand therefore the mouse (when clicking) is far from the TextBlock. However, by setting the Placementproperty to another value should enable a wide range of positions to be achieved. However, setting different values of the Placementproperty alone, will just display the ToolTipin the top left corner of the screen.

这解释了为什么ToolTip显示在远离TextBlock放置目标的地方...因为Button鼠标(单击时)远离TextBlock. 但是,通过将Placement属性设置为另一个值应该能够实现广泛的位置。但是,Placement单独设置不同的属性值,只会ToolTip在屏幕左上角显示 。

To address this situation, you should also set the ToolTip.PlacementTargetProperty, as @icebat correctly noted in the comment, but apparently only from code. Once the PlacementTargetproperty has been set, the Placementproperty value works as expected. From this linked page:

为了解决这种情况,您还应该设置ToolTip.PlacementTargetProperty,正如@icebat 在评论中正确指出的那样,但显然只是从代码中。一旦PlacementTarget属性被设置时,Placement如预期的属性值的作品。从这个链接页面:

You can position a ToolTip by setting the PlacementTarget, PlacementRectangle, Placement, HorizontalOffset, and VerticalOffsetProperty properties.

您可以通过设置 PlacementTarget、PlacementRectangle、Placement、Horizo​​ntalOffset 和 VerticalOffsetProperty 属性来定位工具提示。

enter image description here

在此处输入图片说明