防止wpf数据网格中的空工具提示

The*_*mer 7 wpf datagrid tooltip

我正在制作一个日历程序,主要由WPF组成DataGrid.由于没有足够的空间来显示一天中的所有条目(即a DataGridCell),因此鼠标悬停时会显示包含日期shell的所有条目的工具提示.到目前为止,这可以使用下面显示的代码片段.现在(小)问题:如果一天没有条目,则不会弹出工具提示shell.使用下面的代码,弹出一个空的工具提示.

<DataGridTemplateColumn x:Name="Entry" 
                        IsReadOnly="True">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <Grid>
        <TextBlock Text="{Binding EntryText}"
                   Foreground="{Binding EntryForeground}"
                   FontWeight="{Binding EntryFontWeight}">
        </TextBlock>
        <TextBlock Text="{Binding RightAlignedText}"
                   Foreground="Gray"    
                   Background="Transparent">
          <TextBlock.ToolTip>
            <TextBlock Text="{Binding AllEntriesText}"/>
          </TextBlock.ToolTip>
        </TextBlock>
      </Grid>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)

数据绑定是通过

myCalDataGrid.Itemssource = _listOfDays; 
Run Code Online (Sandbox Code Playgroud)

在后面的代码中,'Day'是单个日历行的视图模型.

Roh*_*ats 11

由于HB建议直接绑定到ToolTip属性而不是使用TextBlock,如果AllEntriesText是空字符串,您可以在TextBlock上应用触发器,通过设置如下属性来禁用工具提示ToolTipService.IsEnabled-

<TextBlock Text="{Binding RightAlignedText}"
           Foreground="Gray"    
           Background="Transparent"
           ToolTip="{Binding AllEntriesText}">
   <TextBlock.Style>
      <Style TargetType="TextBlock">
         <Style.Triggers>
            <Trigger Property="ToolTip"
                     Value="{x:Static system:String.Empty}">
               <Setter Property="ToolTipService.IsEnabled" Value="False" />
            </Trigger>
         </Style.Triggers>
       </Style>
    </TextBlock.Style>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

确保在xaml中添加命名空间系统 -

xmlns:system="clr-namespace:System;assembly=mscorlib"
Run Code Online (Sandbox Code Playgroud)