Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Tooltip Show only when Text is something

Tags:

c#

.net

vb.net

wpf

i am having the following code:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="{x:Type ToolTip}" TargetType="{x:Type ToolTip}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToolTip}">
                    <Border Background="Black">
                        <TextBlock  FontFamily="Tahoma" FontSize="11" Text="{TemplateBinding Content}" Foreground="WhiteSmoke" Padding="2" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<StackPanel>
    <Label Content="Label 1" ToolTip="asd" />
    <Label Content="Label 2" ToolTip="" />
    <TextBlock Text="TextBlock 1" ToolTip="asd" />
    <TextBlock Text="TextBlock 2" ToolTip="" />
    <Button Content="Button 1" ToolTip="asd" />
    <Button Content="Button 2" ToolTip="" />
</StackPanel>

Now, as you can see by testing that when you hover over Label 2, Textblock 2, Button 2, a tooltip still shows. I want that to be triggered that if Tooltip is empty or null then it should not show anything. I know i can simply remove that from XAML But the way i am doing here is something different.

I have tried adding a trigger to check value ="" and to null and inside trigger, setting template to null but none of them is working

If some of you experts could shed some light on it, i would be very glad

like image 588
Walter Boss Avatar asked Jan 05 '13 14:01

Walter Boss


2 Answers

A converter is probably overkill when a DataTrigger can accomplish the same thing. The following style is your posted style, with a bit of clean up and the necessary triggers. Note the following:

  1. I typically set the default style that will eventually be overridden by a trigger. In this case, the default style is Visibility=Visible.
  2. There are two triggers. One for when the content is null and the other when the content is empty.

XAML

<Style TargetType="ToolTip">
    <Setter Property="Visibility" Value="Visible" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Border Background="Black">
                    <TextBlock FontFamily="Tahoma"
                               FontSize="11"
                               Foreground="WhiteSmoke"
                               Padding="2"
                               Text="{TemplateBinding Content}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Value="{x:Null}">
            <Setter Property="Visibility" Value="Collapsed" />
        </DataTrigger>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Value="">
            <Setter Property="Visibility" Value="Collapsed" />
        </DataTrigger>
    </Style.Triggers>
</Style>
like image 52
Metro Smurf Avatar answered Oct 07 '22 14:10

Metro Smurf


You can consider using a IValueConverter to show/hide the tooltip border. Add this class to your project :

class BorderVisibilitySetter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
       //check if the control's content property is null or empty        
        if(value == null || value.ToString() == string.Empty)
            return Visibility.Collapsed;
        else
            return Visibility.Visible;
    }

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

Then modify your xaml as :

<src:BorderVisibilitySetter x:Key="BorderVisible" />
    <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 BorderVisible}}" >
                        <TextBlock  FontFamily="Tahoma" FontSize="11" Text="{TemplateBinding Content}" Foreground="WhiteSmoke" Padding="2" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
like image 40
prthrokz Avatar answered Oct 07 '22 14:10

prthrokz