我已向 WPF 数据网格添加了一个工具提示,以便在鼠标悬停时显示数据。我想知道当仅将鼠标悬停在带有斜线的数据上时是否可以向该工具提示添加文本。
数据的 EX 为:2.34/25 和 22/2
将鼠标悬停在带有斜线的数据上时,工具提示的 EX 为:TEST MSG 2.34/25
</UserControl.Resources>
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}" >
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content.Text}" />
<Setter Property="Height" Value="25" />
</Style>
</UserControl.Resources>
最佳答案
您可以使用两个转换器和数据触发器。
第一个检查文本是否包含斜杠的转换器:
public class SlashTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value.ToString().Contains("/"))
return true;
else
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
格式化自定义消息的第二个转换器:
public class CustomToolTipMessage : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return string.Format("TEST MSG {0}", value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
这是 DataTrigger 的样式:
<Window.Resources>
<local:SlashTextConverter x:Key="slashConverter" />
<local:CustomToolTipMessage x:Key="customToolTipConverter" />
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content.Text}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<DataTrigger Binding="{Binding Path=Content.Text, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource slashConverter}}" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content.Text, Converter={StaticResource customToolTipConverter}}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
关于wpf - 将鼠标悬停在 WPF 中包含斜杠的单元格上时向工具提示添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17951935/