Hide tooltip if binding is null

后端 未结 6 610
情深已故
情深已故 2021-02-01 14:33

Currently i\'ve got the following code to show a tooltip.



        
相关标签:
6条回答
  • 2021-02-01 15:15

    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>
    
    0 讨论(0)
  • 2021-02-01 15:18

    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

    0 讨论(0)
  • 2021-02-01 15:20

    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.

    0 讨论(0)
  • 2021-02-01 15:25

    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);
    
    0 讨论(0)
  • 2021-02-01 15:34

    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"
    
    0 讨论(0)
  • 2021-02-01 15:36

    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.

    0 讨论(0)
提交回复
热议问题