如何让 TreeViewItems 不从其父级继承 Tooltip 属性

发布于 2024-11-26 19:02:55 字数 762 浏览 3 评论 0原文

我有一个应用程序,其中有一个准 IDE,其中 TreeView 充当解决方案资源管理器。用户设计师是一个屏幕布局,可能看起来像这样。

Root
    Menus
        MainMenu
            MenuItem1
    Button Bars
        MainBar
            Button1

我最初对上下文菜单有疑问。在上面的示例中,MenuItem1 没有上下文菜单,但 MainMenu 有。那么,MenuItem1 将从 MainMenu 继承上下文菜单。我通过创建一个空的上下文菜单并将其分配给 MenuItem1 来解决这个问题。不过我想要更优雅的东西。

我对工具提示也有同样的问题。如果我将一项分配给 MainMenu,则 MenuItem1 将继承分配给 MainMenu 的项目。我尝试将 MenuItem1 工具提示设置为 null,但什么也没做。如果我将其设置为“”,则空字符串会覆盖 MainMenu 工具提示,但当您将鼠标悬停在 MenuItem1 上时,会出现一个小的空工具提示框。我认为系统足够聪明,如果它是空字符串,则不会显示该框,但显然不是。

如何防止孩子从父母那里继承上下文菜单和工具提示属性?

更新后


仍有问题。我使用 Snoop 分析了我的项目,它表明这些属性是继承的,但我仍然没有看到任何打破继承的解决方案。

我能想到的唯一的混乱是,对于每个工具提示来处理 ToolTipOpening 事件并检查字符串,如果它没有长度,则立即关闭它。但一定有更好的方法。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

岁月苍老的讽刺 2024-12-03 19:02:55

我遇到了完全相同的问题,但我找到了适合我的解决方案。我更改了工具提示的可见性,使其不再出现空字符串。

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

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;
烟雨凡馨 2024-12-03 19:02:55

您是否尝试过设置 ToolTipService.IsEnabled="False" 这将禁用所需元素上的工具提示。

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

旧情勿念 2024-12-03 19:02:55

对于我自己,我创建了一个宽度高度为零的样式:

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

当我使用此样式创建工具提示并将其放置在资源中时:

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

然后设置此工具提示for every item:

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

或 in style:

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

在这种情况下,item的ToolTip将默认为空,但是当您设置我们的ToolTip时,它将仅为他定义。

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.

蒲公英的约定 2024-12-03 19:02:55

首先,应该使用 null 而不是 string.empty 来屏蔽工具提示。其次,如果您为树视图使用了分层数据模板和 itemssource 绑定,那么您可以根据模板层次结构设置工具提示(例如从模型或 itemssource 绑定到对象层次结构中的属性),在这种情况下它们必须生效基于您的特定树视图项目。

到目前为止,您可以使用 null 来屏蔽。

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.

得不到的就毁灭 2024-12-03 19:02:55

这里的其他答案对我来说都有问题,所以这是我想出的方法,它确实避免了子树项目显示父项目的提示。

与其他一些答案类似,我对 Tooltip 属性使用带有设置器的样式。主要区别在于:

  1. 绑定 ToolTip 元素的 Visibility,而不是显示提示的 TextBlock 元素。
  2. 使用 Border 元素包裹 TextBlock。这避免了偶尔看到一个微小的、空的尖端块。
<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 是我创建的一个简单转换器,它返回 null 或空字符串的 Visibility.Collapsed ,否则返回 Visibility.Visible

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.

夜声 2024-12-03 19:02:55
<[YourControl].Resources>
    <Style TargetType="ToolTip" x:Key="InvisibleToolTip">
        <Setter Property="Visibility"
                Value="Collapsed" />
    </Style>
</[YourControl].Resources>
<[YourControl].ToolTip>
    <ToolTip Style="{StaticResource InvisibleToolTip}"/>
</[YourControl].ToolTip>
<[YourControl].Resources>
    <Style TargetType="ToolTip" x:Key="InvisibleToolTip">
        <Setter Property="Visibility"
                Value="Collapsed" />
    </Style>
</[YourControl].Resources>
<[YourControl].ToolTip>
    <ToolTip Style="{StaticResource InvisibleToolTip}"/>
</[YourControl].ToolTip>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文