开发者

How to get TreeViewItems to not inherit Tooltip property from their parent

I have an application in which is a quasi IDE where a TreeView is acting as a solution explorer. What the user is designer is a screen layout which could look like this.

Root
    Menus
        MainMenu
            MenuItem1
    Button Bars
        MainBar
            Button1

I originally had issues with context menus. In the example above MenuItem1 doesn't have a context menu but MainMenu does. 开发者_JAVA技巧Well, MenuItem1 would inherit the context menu from MainMenu. I got by this by creating an empty context menu and assigning it to MenuItem1. I'd like something more elegant though.

I have the same issues with tooltips. If I assign one to MainMenu then MenuItem1 inherits the one assigned to MainMenu. I tried setting the MenuItem1 tooltip to null, did nothing. If I set it to "", an empty string it overrides the MainMenu tooltip but when you hover over MenuItem1 a small empty tooltip box appears. I thought the system would have been smart enough to not show the box if it was an empty string but apparently not.

How can I prevent children from inheriting context menu and tooltip properties from their parents?

Updated


Still having issues with this. I analyzed my items using Snoop and it indicates that these properties are inheirited, but I still don't see any solution to breaking the inheritance.

The only kludge I can think of is that for every tooltip to handle the ToolTipOpening event and inspect the string, if it has no length then jsut close it immediately. There must be a better way though.


I ran into the exact same problem but I found a solution that works for me. I changed the visibility of the tooltip so that it no longer appears for empty strings.

System.Windows.Controls.ToolTip tt = new System.Windows.Controls.ToolTip();
tt.Content = tooltipDescription;
if (tooltipDescription == null)
    tt.Visibility = Visibility.Collapsed;
item.ToolTip = tt;


Have you tried setting ToolTipService.IsEnabled="False" this will disable your Tooltip on the desired element.


For myself, I created a style 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 I created ToolTip with this style and placed in resources:

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

Then set this ToolTip for each item:

<TreeViewItem Header="Sample" ToolTipService.ToolTip="{StaticResource NoToolTip}">

or in style:

<Setter Property="ToolTipService.ToolTip" Value="{StaticResource NoToolTip}" />

In this case, null ToolTip for item will be default, but when you set our ToolTip, it will be defined only for him.


Firstly masking a tooltip should be done with null and not string.empty. Secondly if you had used hierarchical data template and itemssource binding for your treeview then you could have set tooltips based on your template hierachy (such as bound to a property in your object hierarchy from model or itemssource) in which case they must had taken an effect based on your specific treeview item.

As of now you can use null to mask.


The other answers here all had issues for me so here's the approach I came up with which does avoid child tree items showing the parent item's tip.

Similar to some other answers, I use a style with a setter for the Tooltip property. The key differences are:

  1. Binding the Visibility of a ToolTip element instead of the TextBlock element showing the tip.
  2. Wrapping the TextBlock with a Border element. This avoided occasionally seeing a tiny, empty tip block.
<local:StringToVisibilityConverter x:Key="strToVisibilityConverter"/>

<Style x:Key="MyTreeStyleKey" TargetType="TreeViewItem">
  <Setter Property="ToolTip">
    <Setter.Value>
      <ToolTip Visibility="{Binding TipText, Converter={StaticResource strToVisibilityConverter}}">
        <Border>
          <TextBlock Text="{Binding TipText}"/>
        </Border>
      </ToolTip>
    </Setter.Value>
  </Setter>
</Style>

StringToVisibilityConverter is a simple converter I created which returns Visibility.Collapsed for null or emptry strings, Visibility.Visible otherwise.


<[YourControl].Resources>
    <Style TargetType="ToolTip" x:Key="InvisibleToolTip">
        <Setter Property="Visibility"
                Value="Collapsed" />
    </Style>
</[YourControl].Resources>
<[YourControl].ToolTip>
    <ToolTip Style="{StaticResource InvisibleToolTip}"/>
</[YourControl].ToolTip>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜