George Hernando
George Hernando

Reputation: 2630

XAML for WPF: Hide ToolTip Popup when blank Tooltip text

I'm trying to set a ToolTip on the cell of a WPF form with a Datagrid. That works, but I don't want it to pop up for cells where there is no popup text to display.

I've seen similar questions asked here, but I haven't been able to get those solutions working.

This is the CellTemplate:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Image Source="{Binding itemType}" VerticalAlignment="Center">
            <ToolTipService.ToolTip>
                    <TextBlock Text="{Binding toolTipText}" />
            </ToolTipService.ToolTip>
        </Image>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

To prevent the popping up on blank toolTipText, I've added:

<DataGrid.Resources>
<Style x:Key="{x:Type ToolTip}" TargetType="{x:Type ToolTip}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToolTip}">
                <Border Background="Black" Visibility="{TemplateBinding Content, Converter={StaticResource StringToVisibilityConverter}}" >
                    <TextBlock  Width="50" FontFamily="Tahoma" FontSize="11" Text="{TemplateBinding Content}" Foreground="WhiteSmoke" Padding="2" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</DataGrid.Resources>

StringToVisibilityConverter is defined as follows:

public class StringToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        var stringValue = String.Empty;
        if (value is System.Windows.Controls.TextBlock) stringValue = (value as System.Windows.Controls.TextBlock).Text;
        else stringValue = value as string;
        return string.IsNullOrWhiteSpace(stringValue) ?
                                         Visibility.Hidden : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        return value;
    }
}

What I'm seeing is that it isn't popping up the Tooltip when the text is blank (good), but when there is text, it pops up a black rectangle.
Without the Style setting for the tooltip the text box pops up OK (except it pops up for blank text too).

When I remove

Background="Black"

on the border styling then I don't see anything popup.

Upvotes: 6

Views: 4674

Answers (2)

stuicidle
stuicidle

Reputation: 317

You could try editing the ToolTip template and add Triggers to collapse the ToolTip if it has no content. If you add the template to your App.xaml file this also has the benefit of hiding all empty ToolTips that may be present in your application.

in App.xaml, add a reference to Microsoft core libriarys:

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

then add your new ToolTip Template:

<Style x:Key="{x:Type ToolTip}" TargetType="ToolTip">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="HasDropShadow" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Border Name="Border" Background="#FFFFFF" BorderBrush="#000000" BorderThickness="1" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                    <ContentPresenter Margin="4" HorizontalAlignment="Left" VerticalAlignment="Top" />
                </Border>
                <ControlTemplate.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>
                    <Trigger Property="HasDropShadow" Value="true">
                        <Setter TargetName="Border" Property="CornerRadius" Value="4"/>
                        <Setter TargetName="Border" Property="SnapsToDevicePixels" Value="true"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Upvotes: 1

Nikita Shrivastava
Nikita Shrivastava

Reputation: 3018

You can make it simpler with the below celltemplate:

<DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Source="{Binding itemType}" VerticalAlignment="Center">
             <Image.ToolTip>
                <ToolTip Visibility="{Binding toolTipText, Converter={StaticResource StringToVisibilityConverter}}">
                  <Border Background="Black" >
                    <TextBlock Text="{Binding toolTipText}" />
                  </Border>
               </ToolTip>                    
            <Image.ToolTip>
          </Image>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>

And the converter can be simplified to :

public class StringToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return String.IsNullOrEmpty(value as string) ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 7

Related Questions