Tooltip programming

I developed a WPF sample project.

Here is the basic layout of the XAML window:

<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 code without using operators:

 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 a tooltip when I click a button.
I want the tooltip to appear above the TextBlock parent.

A tooltip is automatically displayed when the mouse cursor is over a TextBlock and for a constant time C is approximately 5 seconds.
I want the tooltip to appear during C when the button is pressed.

My goals are not achieved in the current project.
A tooltip is displayed when the button is pressed:

enter image description here

But:

  • ToolTip is too far from the parent TextBlock.
  • tooltip will not be automatically hidden

What do I need to do to achieve my goals?
Any help would be greatly appreciated.

+6
source share
2 answers

I have achieved my goals.
Sheridan's answer and comments help me.
I updated my sample project.
Tooltip now appears just above TextBlock when a button is clicked.
And the timer callback method closes the prompt after a constant amount of time equal to 2500 ms.

Here is the updated basic XAML window layout:

 <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 code without using operators:

 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; }); } } } 
+2
source

You have a number of problems. The first and easiest thing is that you open and do not close ToolTip . You said that I want the tooltip to show for the same amount of time the button is clicked, and this is easy to implement using the PreviewMouseDown and PreviewMouseUp events:

 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 a little more difficult to fix ... it seems to me that there may be an error related to the positioning of your ToolTip . Usually, despite what @icebat said, you can reposition the ToolTip using the ToolTip.Placement property . This can be set to one of the PlacementMode Enumerations .

The default value is Mouse , and this is the definition from the linked page on MSDN:

Place a Popup control that aligns its top edge with the bottom edge of the mouse's bounding box and aligns the left edge with the left edge of the bounding box of the mouse. If the bottom edge of the screen hides the popup, it is rebuilt to align it with the top edge of the mouse bounding box. If the top edge of the screen hides the popup, the control rebuilds itself to align with the top edge of the screen.

This explains why the ToolTip displayed far from the TextBlock placement target ... because the Button and therefore the mouse (when clicked) is far from the TextBlock . However, setting the Placement property to a different value requires a wide range of positions to be achieved. However, by setting different values ​​for the Placement property, only the ToolTip will be displayed in the upper left corner of the screen.

To resolve this situation, you should also set the ToolTip.PlacementTarget Property , as @icebat was correctly noted in the comment, but apparently only from the code. After the PlacementTarget property is set, the value of the Placement property works as expected. On this page:

You can place a hint by setting the properties of PlacementTarget, PlacementRectangle, Placement, HorizontalOffset and VerticalOffsetProperty.

enter image description here

+7
source

All Articles