以编程方式显示工具提示 [英] Programmatically showing a ToolTip

查看:22
本文介绍了以编程方式显示工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have developed a WPF sample project.

这是主窗口的 XAML 标记:

Here is the main Window's XAML markup :

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

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

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

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

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

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

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

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.

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

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

但是:

  • 工具提示与其 TextBlock 父级相距太远.
  • 工具提示不会自动隐藏

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

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

推荐答案

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

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.

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

Here is the updated main Window's XAML markup :

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

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

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

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;
                });
        }
    }
}

这篇关于以编程方式显示工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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