适用于 WPF 的 XAML:当空白工具提示文本时隐藏工具提示弹出窗口 [英] XAML for WPF: Hide ToolTip Popup when blank Tooltip text

查看:24
本文介绍了适用于 WPF 的 XAML:当空白工具提示文本时隐藏工具提示弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Datagrid 在 WPF 表单的单元格上设置工具提示.这行得通,但我不希望它在没有可显示弹出文本的单元格中弹出.

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.

这是细胞模板:

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

为了防止在空白 toolTipText 上弹出,我添加了:

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 定义如下:

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

当我删除时

Background="Black"

在边框样式上,我看不到任何弹出窗口.

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

推荐答案

您可以使用以下单元格模板使其更简单:

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>

转换器可以简化为:

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();
    }
}

这篇关于适用于 WPF 的 XAML:当空白工具提示文本时隐藏工具提示弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆