如果绑定为空,则隐藏工具提示

发布于 2024-11-05 14:52:26 字数 561 浏览 0 评论 0原文

目前我有以下代码来显示工具提示。

<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 技术交流群。

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

发布评论

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

评论(7

血之狂魔 2024-11-12 14:52:26

隐藏所有控件的空工具提示的一种方法是在 App.xaml 中包含的资源字典中创建样式。
当工具提示为空字符串或 null 时,此样式将可见性设置为折叠:

<!-- Style to hide tool tips that have an empty content. -->
<Style TargetType="ToolTip">
    <Style.Triggers>
        <Trigger Property="Content"
                 Value="{x:Static sys:String.Empty}">
            <Setter Property="Visibility"
                    Value="Collapsed" />
        </Trigger>
        <Trigger Property="Content"
                 Value="{x:Null}">
            <Setter Property="Visibility"
                    Value="Collapsed" />
        </Trigger>
    </Style.Triggers>
</Style>

还包括 sys 命名空间(对于 String.Empty):

xmlns:sys="clr-namespace:System;assembly=mscorlib"

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:

<!-- Style to hide tool tips that have an empty content. -->
<Style TargetType="ToolTip">
    <Style.Triggers>
        <Trigger Property="Content"
                 Value="{x:Static sys:String.Empty}">
            <Setter Property="Visibility"
                    Value="Collapsed" />
        </Trigger>
        <Trigger Property="Content"
                 Value="{x:Null}">
            <Setter Property="Visibility"
                    Value="Collapsed" />
        </Trigger>
    </Style.Triggers>
</Style>

Also include sys namespace (for String.Empty):

xmlns:sys="clr-namespace:System;assembly=mscorlib"
橪书 2024-11-12 14:52:26

一种方法是将ToolTip 包装在矩形 中,并为其指定透明 颜色。然后,您只需在此矩形 上将Visibility 设置为Collapsed 即可。

更新:

<Border Background="#FFE45F5F">
    <Grid>
        <TextBlock Text="{Binding Property1}"/>
        <Rectangle Fill="Transparent" Visibility="{Binding Property2, Converter={StaticResource BooleanToVisibilityConverter}}" ToolTipService.ToolTip="{Binding TooltipInformation}"/>
    </Grid>
</Border>

One way you can do that is wrap the ToolTip in a Rectangle and give it a Transparent color. Then you just set the Visibility to Collapsed on this Rectangle.

Update:

<Border Background="#FFE45F5F">
    <Grid>
        <TextBlock Text="{Binding Property1}"/>
        <Rectangle Fill="Transparent" Visibility="{Binding Property2, Converter={StaticResource BooleanToVisibilityConverter}}" ToolTipService.ToolTip="{Binding TooltipInformation}"/>
    </Grid>
</Border>
把昨日还给我 2024-11-12 14:52:26

这是一个 WPF 答案(尚未在 Silverlight 中尝试过)。

使用 ToolTipService.IsEnabled,并将其绑定到工具提示属性。然后使用转换器将工具提示字符串转换为布尔值。

例如,我有以下内容:

<TextBlock x:Name="textBlock" ToolTipService.IsEnabled="{Binding EntryToolTip, Converter={StaticResource StringNullOrEmptyToBoolConverter}}">
...
</TextBlock>

或者在代码隐藏中

ToolTipService.SetIsEnabled(textBlock, false);

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:

<TextBlock x:Name="textBlock" ToolTipService.IsEnabled="{Binding EntryToolTip, Converter={StaticResource StringNullOrEmptyToBoolConverter}}">
...
</TextBlock>

Or in code-behind

ToolTipService.SetIsEnabled(textBlock, false);
韵柒 2024-11-12 14:52:26

当我将值设置为 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

べ映画 2024-11-12 14:52:26

如果仅使用默认工具提示,我会建议在视图模型中将绑定值设置为 null 或在项目为空时使用转换器。

在我的例子中,我有一个:

public string Name { get; }

绑定使用:

<TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis" Tooltip="{Binding Name}" />

如果由于宽度不足而被剪切,则在工具提示中显示全名。在我的视图模型中,我简单地:

if (string.IsNullOrEmpty(Name)) Name = 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:

public string Name { get; }

Bound using:

<TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis" Tooltip="{Binding Name}" />

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:

if (string.IsNullOrEmpty(Name)) Name = null;

At least in .Net 4.0 this will not show a tooltip for me.

固执像三岁 2024-11-12 14:52:26

奇怪的是,这些答案对我来说都不起作用。对最佳答案的回复暗示了这一点 - 如果您的 ToolTip 与 TextBlock 相关,那么该解决方案将不起作用。我在 DataGridTemplateColumn.CellTemplate 元素中有一个 TextBlock,我只是将文本直接绑定到 TextBlock 的 ToolTip 属性,如下所示:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            ToolTip="{Binding Path=SomeTextProperty}"
            Style="{StaticResource TextBlockOverflowStyle}"
            Text="{Binding Path=SomeTextProperty, NotifyOnSourceUpdated=True}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

我最终“免费”获得了所需的行为(文本为空时隐藏工具提示)。

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:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            ToolTip="{Binding Path=SomeTextProperty}"
            Style="{StaticResource TextBlockOverflowStyle}"
            Text="{Binding Path=SomeTextProperty, NotifyOnSourceUpdated=True}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

And I ended up getting the desired behavior (hidden tooltip when text is empty) "for free".

挽清梦 2024-11-12 14:52:26

您可以创建一个从字符串到布尔值的转换器,如果字符串长度为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文