2

我在我的 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>
4

1 回答 1

3

您可以使用两个转换器和数据触发器。

第一个检查文本是否包含斜线的转换器:

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>
于 2013-07-30T17:15:05.947 回答