我正在尝试使用 Datagrid 在 WPF 表单的单元格上设置工具提示。
这可行,但我不希望它在没有可显示的弹出文本的单元格中弹出。
我在这里看到过类似的问题,但我无法让这些解决方案发挥作用。
这是单元格模板:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding itemType}" VerticalAlignment="Center">
<ToolTipService.ToolTip>
<TextBlock Text="{Binding toolTipText}" />
</ToolTipService.ToolTip>
</Image>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
为了防止在空白工具提示文本上弹出,我添加了:
<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 定义如下:
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;
}
}
我看到的是,当文本为空白时(良好),它不会弹出工具提示,但是当有文本时,它会弹出一个黑色矩形。
如果没有工具提示的样式设置,文本框会正常弹出(除了空白文本也会弹出)。
当我删除时
Background="Black"
在边框样式上,我没有看到任何弹出内容。