如何将工具提示的可见性绑定到文本块中的文本? [英] How to bind the visibility of tooltip to the text in textblock?

查看:25
本文介绍了如何将工具提示的可见性绑定到文本块中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<DataGrid x:Name="dgRecords1"
          CanUserAddRows="False" IsReadOnly="True"
          ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}"
          Style="{DynamicResource StyleDatagrid}"
          SelectionChanged="dgRecords1_SelectionChanged"
          Height="251" Width="569" Margin="41,173,168,0">
  <DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
      <Setter Property="ToolTip">
        <Setter.Value>
          <Border Width="200" Height="80"
                  BorderBrush="Black" BorderThickness="1"
                  Background="AliceBlue">
            <StackPanel Orientation="Vertical">
              <StackPanel Height="30" Background="Black">
                <TextBlock Text="Email Sent To"
                           FontSize="14" FontWeight="Bold" Foreground="White"/>
              </StackPanel>
              <StackPanel>
                <TextBlock Text="{Binding SentToList}"
                           TextWrapping="Wrap" FontWeight="Bold"/>
              </StackPanel>
            </StackPanel>
          </Border>
        </Setter.Value>
      </Setter>
    </Style>
  </DataGrid.RowStyle>

在上面的代码中,

   <TextBlock TextWrapping="Wrap" FontWeight="Bold" Text="{Binding SentToList}" />

我想检查一下这个textblock中是否有东西,如果没有,我需要让tooltip不可见.有没有办法使用触发器来做到这一点?

I want to check whether there is something in this textblock, and if there is nothing, I need to make the tooltip invisible. Is there some way of doing it using Triggers?

推荐答案

你可以用 ToolTip 控件包围你的边框,并通过使用 绑定转换器 将字符串转换为可见性.

You could surround your Border by a ToolTip control and bind the Visibility of that control to the same SentToList property by using a binding converter that converts from string to Visibility.

<Style TargetType="DataGridRow">
    <Style.Resources>
        <local:StringToVisibilityConverter x:Key="StringToVisibilityConverter"/>
    </Style.Resources>
    <Setter Property="ToolTip">
        <Setter.Value>
            <ToolTip Visibility="{Binding SentToList, Converter={StaticResource StringToVisibilityConverter}}">
                <Border>
                    ...
                </Border>
             </ToolTip>
        </Setter.Value>
    </Setter>
</Style>

转换器可能如下所示:

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

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

这篇关于如何将工具提示的可见性绑定到文本块中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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