如果绑定为空,则隐藏工具提示
目前我有以下代码来显示工具提示。
<Border BorderBrush="Black"
BorderThickness="{Binding Border}"
Height="23"
Background="{Binding Color}">
<ToolTipService.ToolTip>
<TextBlock Text="{Binding TooltipInformation}" />
</ToolTipService.ToolTip>
它显示在包含大约 25 个项目的 ItemsControl 中。其中只有少数将值设置为 TooltipInformation
如果 TooltipInforation
是空字符串,它仍然将包含文本块的工具提示框显示为一个非常小的窗口(大约 5 像素高, 20 像素宽)。即使我将文本块可见性设置为折叠。
如果 TooltipInformation 的值为 null 或空字符串,是否有办法完全删除工具提示?
Currently i've got the following code to show a tooltip.
<Border BorderBrush="Black"
BorderThickness="{Binding Border}"
Height="23"
Background="{Binding Color}">
<ToolTipService.ToolTip>
<TextBlock Text="{Binding TooltipInformation}" />
</ToolTipService.ToolTip>
This is presented in a ItemsControl with about 25 items. Only a few of these have a value set to TooltipInformation
If TooltipInforation
is an empty string, it still shows the tooltipbox containing the textblock as a very small window (about 5px high and 20px wide). Even if I set the textblock visbility to collapsed.
Is there a way to completely remove the tooltip if the value of TooltipInformation is null or a empty string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
隐藏所有控件的空工具提示的一种方法是在 App.xaml 中包含的资源字典中创建样式。
当工具提示为空字符串或 null 时,此样式将可见性设置为折叠:
还包括 sys 命名空间(对于 String.Empty):
One way to hide an empty tooltip for all controls is to create a style in a resource dictionary that is included in your App.xaml.
This style sets the visibility to collapsed when the tooltip is an empty string or null:
Also include sys namespace (for String.Empty):
一种方法是将
ToolTip
包装在矩形
中,并为其指定透明
颜色。然后,您只需在此矩形
上将Visibility
设置为Collapsed
即可。更新:
One way you can do that is wrap the
ToolTip
in aRectangle
and give it aTransparent
color. Then you just set theVisibility
toCollapsed
on thisRectangle
.Update:
这是一个 WPF 答案(尚未在 Silverlight 中尝试过)。
使用 ToolTipService.IsEnabled,并将其绑定到工具提示属性。然后使用转换器将工具提示字符串转换为布尔值。
例如,我有以下内容:
或者在代码隐藏中
This is a WPF answer (haven't tried it in Silverlight).
Use ToolTipService.IsEnabled, and bind it to the tooltip property. Then use a converter to convert the tooltip string to a bool.
For example, I have the following:
Or in code-behind
当我将值设置为 String.Empty 时,我遇到了同样的问题。将其设置为 null 可以解决该问题。
WinRT/Windows 8 应用程序 XAML
I was having the same issue as I was setting value to String.Empty. Setting it to null solves the problem.
WinRT/Windows 8 App XAML
如果仅使用默认工具提示,我会建议在视图模型中将绑定值设置为 null 或在项目为空时使用转换器。
在我的例子中,我有一个:
绑定使用:
如果由于宽度不足而被剪切,则在工具提示中显示全名。在我的视图模型中,我简单地:
至少在 .Net 4.0 中,这不会为我显示工具提示。
If just using the default tooltip I would otherwise recommend either setting the bound value to null in the viewmodel or using a converter whenever the item is empty.
In my case I've got a:
Bound using:
Where the idea is to show the full name in the tooltip if cut of due to lack of width. In my viewmodel I simply:
At least in .Net 4.0 this will not show a tooltip for me.
奇怪的是,这些答案对我来说都不起作用。对最佳答案的回复暗示了这一点 - 如果您的 ToolTip 与 TextBlock 相关,那么该解决方案将不起作用。我在 DataGridTemplateColumn.CellTemplate 元素中有一个 TextBlock,我只是将文本直接绑定到 TextBlock 的 ToolTip 属性,如下所示:
我最终“免费”获得了所需的行为(文本为空时隐藏工具提示)。
Strangely, none of these answers worked in my case. A reply to the top answer alludes to it - if you're ToolTip is related to a TextBlock then that solution won't work. I have a TextBlock within a DataGridTemplateColumn.CellTemplate element, and I just bound the text directly to the ToolTip property of the TextBlock like this:
And I ended up getting the desired behavior (hidden tooltip when text is empty) "for free".
您可以创建一个从字符串到布尔值的转换器,如果字符串长度为 0,则返回 false,否则返回 true,然后使用该转换器将 ToolTip.Active 绑定到 TooltipInformation。
You could create a converter from string to bool that returns false if the string length is 0 and true otherwise, then bind ToolTip.Active to TooltipInformation with that converter.