ToolTip in TabItem: show title, but not by content

Tooltips in TabControl TabItems are displayed not only in the TabItem header, but also on any TabItem content that does not explicitly set its own tooltip.

Here is an example that reproduces the problem:

<Window x:Class="TestToolTipsOnTabControl.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <Grid> <TabControl> <TabItem Header="Tab1" ToolTip="Tooltip of tab1"> <StackPanel> <TextBlock Text="Content of tab1 with its own tooltip" ToolTip="Tooltip on content of tab1"/> <TextBlock Text="more content of tab1" /> </StackPanel> </TabItem> <TabItem Header="Tab2" ToolTipService.ToolTip="Tooltip of tab2"> <StackPanel> <TextBlock Text="Content of tab2 with its own tooltip" ToolTipService.ToolTip="Tooltip on content of tab2"/> <TextBlock Text="more content of tab2" /> </StackPanel> </TabItem> <TabItem Header="Tab3"> <StackPanel> <TextBlock Text="Content of tab3" /> <TextBlock Text="more content of tab3" /> </StackPanel> </TabItem> </TabControl> </Grid> </Window> 

Moving the mouse over the text "more content of tab1" will display a tooltip that I would like to only display in the TabItem header.

Is there a way for the ToolTip to appear only in the TabItem header, but nowhere else?

+6
source share
5 answers

Is there a way for the ToolTip to appear only in the TabItem header, but nowhere else?

You should only apply the Tooltip to the Header not to the whole TabItem , so change it to:

  <TabItem> <TabItem.Header> <TextBlock Text="Tab1" ToolTip="Tooltip of tab1"/> </TabItem.Header> <StackPanel> <TextBlock Text="Content of tab1 with its own tooltip" ToolTip="Tooltip on content of tab1"/> <TextBlock Text="more content of tab1" /> </StackPanel> </TabItem> 
+7
source

Add the following code snippet to text blocks

ToolTip = ", ToolTipSevice.ShowDuration =" 0 "

+1
source

As an alternative, I can suggest the following: created a Style for ToolTip with zero Width and Height :

 <Style x:Key="NullToolTip" TargetType="{x:Type ToolTip}"> <Setter Property="Width" Value="0" /> <Setter Property="Height" Value="0" /> <Setter Property="Content" Value="{x:Null}" /> </Style> 

When creating a ToolTip using this Style and placed in Resources :

 <ToolTip x:Key="NoToolTip" Style="{StaticResource NullToolTip}" /> 

And assign the control that ToolTip wants to disable:

 <TabItem Header="Tab1" ToolTip="Tooltip of tab1"> <StackPanel> <TextBlock Text="Content of tab1 with its own tooltip" ToolTip="Tooltip on content of tab1"/> <TextBlock Text="more content of tab1" ToolTipService.ToolTip="{StaticResource NoToolTip}" /> </StackPanel> </TabItem> 

Note. A similar problem occurs if you use ToolTip for TreeViewItem . His children inherit the ToolTip parent.

0
source

Just in case, someone encounters this problem when creating tabs dynamically, the following code did the magic:

  TabItem tabItem = new TabItem(); var stackPanel = new StackPanel(); var stackPanelToolTip = new System.Windows.Controls.ToolTip(); stackPanelToolTip.Content = "ToolTip content"; stackPanel.ToolTip = (stackPanelToolTip); tabItem.Header = stackPanel; 

Thus, the key point was adding a tooltip to the tab title, but not to the tab element (adding it to the title means that you will see the tooltip only when the mouse hangs the tab).

0
source

This works for me when dynamically adding tabItems:

 TabItem nt = new TabItem string _newTabItemText = "xxxx" // Dynamic header text string _newTabItemTooltip = "xxxx Tooltip" // Dynamic tooltip text string _newTabItemName = "xxxx" // tabItem name to reference the tab item in code ie XAML x:name = "xxxx" { Header = new TextBlock() { Text = _newTabItemText, ToolTip = _newTabItemTooltip }, Name = _newTabItemName, Width = 108 }; 
0
source

Source: https://habr.com/ru/post/948525/


All Articles