您可以使用两个转换器和数据触发器。
第一个检查文本是否包含斜线的转换器:
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>