WPF 工具提示仅在文本为某些内容时显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14172846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:54:47  来源:igfitidea点击:

WPF Tooltip Show only when Text is something

c#.netwpfvb.net

提问by Walter Boss

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.

现在,正如您通过测试看到的那样,当您将鼠标悬停在标签 2、文本块 2、按钮 2 上时,仍然会显示工具提示。我希望触发它,如果 Tooltip 为空或 null,那么它不应该显示任何内容。我知道我可以简单地从 XAML 中删除它但是我在这里做的方式有所不同。

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

我尝试添加一个触发器来检查值 ="" 和 null 和内部触发器,将模板设置为 null 但它们都不起作用

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

如果你们中的一些专家可以对此有所了解,我会很高兴

回答by Metro Smurf

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:

当 DataTrigger 可以完成同样的事情时,转换器可能是矫枉过正的。以下样式是您发布的样式,有一些清理和必要的触发器。请注意以下事项:

  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.
  1. 我通常会设置最终会被触发器覆盖的默认样式。在这种情况下,默认样式是Visibility=Visible
  2. 有两个触发器。一种用于内容为空时,另一种用于内容为空时。

XAML

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>

回答by prthrokz

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

您可以考虑使用 IValueConverter 来显示/隐藏工具提示边框。将此类添加到您的项目中:

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 :

然后将您的 xaml 修改为:

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

回答by Palash Roy

My solution will have below features:

我的解决方案将具有以下功能:

  1. Tooltip will be displayed if textblock has some text
  2. Tooltip text is same as textblock text
  3. Tooltip foreground is same as textblock foreground
  1. 如果文本块有一些文本,将显示工具提示
  2. 工具提示文本与文本块文本相同
  3. 工具提示前景与文本块前景相同

Xaml Code:

Xml代码:

<TextBlock Name="TxtBlockLicenseInfo" Height="35"  ToolTipService.ShowDuration="200000" TextTrimming="CharacterEllipsis">
     <TextBlock.ToolTip>
        <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}" 
                                    Visibility="{Binding Text,Converter={StaticResource TextToTooltipVisibilityConverter}}" >
                <StackPanel>
                      <Label Content="{Binding Text}" Foreground="{Binding Foreground}" >
                      </Label>
                </StackPanel>
         </ToolTip>
      </TextBlock.ToolTip>
</TextBlock>

Also include the converter in the Xaml resources

还将转换器包含在 Xaml 资源中

<UserControl.Resources>
    <local:LicenseInformationToTooltipVisibilityConverter x:Key="LicenseInformationToTooltipVisibilityConverter" />
</UserControl.Resources>

Xaml.Cs Code to write the converter

Xaml.Cs 编写转换器的代码

public class TextToTooltipVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((value.ToString().Equals(string.Empty)))
        {
            return Visibility.Collapsed;
        }
        else return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Visibility.Visible;
    }
}