定义一个ToolTip样式,如果Converter(ToolTip)返回fale,它将隐藏工具提示

时间:2016-01-30 22:07:05

标签: wpf xaml binding styles tooltip

我想隐藏空工具提示

这是达到某一点的原因:

  <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>

问题是:触发器仅对null或string.IsEmpty进行操作 我也想抓住空白工具提示。

所以我试过了:

  <Style TargetType="ToolTip">
    <Setter Property="Visibility">
      <Setter.Value>
        <Binding >   <!------------ what comes here? -->
          <Binding.Converter>
            <loc:NotEmptyToVisibilityConverterEx/>
          </Binding.Converter>
        </Binding>
      </Setter.Value>
    </Setter>
  </Style>

我的问题是,我无法(不知道如何)访问实际的工具提示文本。

  [ValueConversion(typeof(string), typeof(Visibility))]
  public class NotEmptyToVisibilityConverterEx : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
      var visible = !string.IsNullOrEmpty(((string) value).Trim());
      return visible ? Visibility.Visible : Visibility.Collapsed;
    }
  }

有人有想法吗?

1 个答案:

答案 0 :(得分:1)

这应该有效:

<Window x:Class="HideEmptyToolTipsWithTrigger.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:HideEmptyToolTipsWithTrigger"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:EmptyToolTipToBooleanConverter x:Key="EmptyToolTipToBooleanConverter" />

        <Style TargetType="ToolTip">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Content, RelativeSource={RelativeSource Self}, Converter={StaticResource EmptyToolTipToBooleanConverter}}" Value="True">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <StackPanel>
        <TextBlock Text="This TextBlock has ToolTip" ToolTip="Almafa"></TextBlock>

        <TextBlock Text="This TextBlock has ToolTip with whitespaces" ToolTip="     "></TextBlock>

        <TextBlock Text="This TextBlock has ToolTip with empty string" ToolTip="{x:Static system:String.Empty}"></TextBlock>
    </StackPanel>
</Window>

转换器:

public class EmptyToolTipToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var contentAsString = value as string;

        if (contentAsString != null)
        {
            return string.IsNullOrEmpty(contentAsString.Trim());
        }
        else
        {
            return false;
        }
    }

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

请注意,ToolTip可以包含任何自定义内容,而不仅仅是string。 例如:

<TextBlock Text="This TextBlock has a complex ToolTip">
    <TextBlock.ToolTip>
        <StackPanel>
            <TextBlock Text="First line"></TextBlock>
            <TextBlock Text="Second line"></TextBlock>
        </StackPanel>
    </TextBlock.ToolTip>
</TextBlock>

此转换器显示这些工具提示。