如何让 TreeViewItems 不从其父级继承 Tooltip 属性
我有一个应用程序,其中有一个准 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我遇到了完全相同的问题,但我找到了适合我的解决方案。我更改了工具提示的可见性,使其不再出现空字符串。
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.
您是否尝试过设置
ToolTipService.IsEnabled="False"
这将禁用所需元素上的工具提示。Have you tried setting
ToolTipService.IsEnabled="False"
this will disable your Tooltip on the desired element.对于我自己,我创建了一个宽度和高度为零的样式:
当我使用此样式创建工具提示并将其放置在资源中时:
然后设置此工具提示for every item:
或 in style:
在这种情况下,item的ToolTip将默认为空,但是当您设置我们的ToolTip时,它将仅为他定义。
For myself, I created a style with zero Width and Height:
When I created ToolTip with this style and placed in resources:
Then set this ToolTip for each item:
or in style:
In this case, null ToolTip for item will be default, but when you set our ToolTip, it will be defined only for him.
首先,应该使用 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.
这里的其他答案对我来说都有问题,所以这是我想出的方法,它确实避免了子树项目显示父项目的提示。
与其他一些答案类似,我对
Tooltip
属性使用带有设置器的样式。主要区别在于:ToolTip
元素的Visibility
,而不是显示提示的TextBlock
元素。Border
元素包裹TextBlock
。这避免了偶尔看到一个微小的、空的尖端块。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:Visibility
of aToolTip
element instead of theTextBlock
element showing the tip.TextBlock
with aBorder
element. This avoided occasionally seeing a tiny, empty tip block.StringToVisibilityConverter
is a simple converter I created which returnsVisibility.Collapsed
for null or emptry strings,Visibility.Visible
otherwise.