开发者

How do I hide the empty tooltip in Silverlight?

I have a TextBlock with a tooltip that displays the same data, in case of truncation. However if the property that TextBlock.Text and the tooltip's text are bound to is empty (null or zero length string) the tooltip appears as a small empty box. Is there a way to hide this and show no tooltip in this case?

<TextBlock Text="{Binding Text}">
    <util:ToolTipManager.ToolTip>
        <TextBlock TextWrapping="Wrap" Text="{Binding Text}" />
    </util:ToolTipManager.ToolTip>
</TextBlock>

I have tried using a StringToVisibilityConverter by adding Visibility="{Binding Text, Converter={StaticResource StringToVisConverter}}" to the TextBlock without any luck.

I also tried implementing the answer given Hide tooltip if binding is null but that seems specific to their set-up (or at least I haven't figured out how to adapt it successfully).

(ToolTipManager is from http://www.codeproject.com/Articles/36078/Silverlight-2-0-How-to-use-a-DataBinding-with-the, used to provide the data binding for the tooltip.)

Edit:

In response to the comments, here is the XAML I tried for the related question mentioned above:

    <TextBlock Text="{Binding PointName}">
        <local:ToolTipManager.ToolTip>
            <Grid>
                <TextBlock TextWrapping="Wrap" Text="{Binding PointName}"/>
                <Rectangle Fill="Transparent"  Visibility="{Binding PointName, Converter={StaticResource StringToVisConverter}}" />
            </Grid>
        </local:ToolTipManager.ToolTip>
    </TextBlock>

And here is my String to Visibility converter code:

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string visible = (string)value;
        retur开发者_运维技巧n (!String.IsNullOrWhiteSpace(visible) ? Visibility.Visible : Visibility.Collapsed);
    }


I assume you are using Siverlight 4 as it is one of your tags.

In Silverlight 4, I am pretty sure that you don't need the ToolTipManager anymore.

You can just wrap the Rectangle and the TextBlock with a Grid, like this,

        <Grid> 
            <TextBlock Text="{Binding PointName}"/> 
            <Rectangle Fill="Transparent" Visibility="{Binding PointName, Converter={StaticResource BooleanToVisibilityConverter}}" ToolTipService.ToolTip="{Binding PointName}"/> 
        </Grid> 

UPDATE:

        <Grid> 
            <TextBlock Text="{Binding PointName}"/> 
            <Rectangle Fill="Transparent" Visibility="{Binding PointName, Converter={StaticResource BooleanToVisibilityConverter}}">
                <ToolTipService.ToolTip>
                    <TextBlock TextWrapping="Wrap" Text="{Binding PointName}"/>             
                </ToolTipService.ToolTip>                       
            </Rectangle> 
        </Grid> 


If you are using Silverlight 5, the following seems to work:

<ToolTipService.ToolTip>
    <ToolTip Visibility="{Binding WhatDeterminesTooltipVisibility}">
        <Border Background="Azure" Width="100" />
    </ToolTip>
</ToolTipService.ToolTip>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜