Search code examples
wpftooltip

Programmatically showing a ToolTip


I have developed a WPF sample project.

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>

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

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

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 :

enter image description here

But :

  • The ToolTip is too far from its TextBlock parent.
  • The ToolTip is not automatically hidden

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


Solution

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

    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>
    

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