I have add a tool tip to my WPF datagrid to show data when the mouse is hovered over. I was wondering if it would be possible to also add text to that ToolTip when only hovering over data that has a slash in it.
EX of data would be: 2.34/25 and 22/2
EX of ToolTip when hovering over data with a slash would be: 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>
You can use two converters and data trigger.
First converter to check if text contains slash:
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();
}
}
Second converter to format custom message:
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();
}
}
And here is style with 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>