wpf - 将鼠标悬停在 WPF 中包含斜杠的单元格上时向工具提示添加文本

标签 wpf c#-4.0 wpf-controls wpfdatagrid

我已向 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/

相关文章:

c# - RegionInfo 在 Windows 8.1 应用程序中引发异常

c# - C# 4.0 中的方法重载与可选参数

c# - C#中如何显示当前时间和日期

WPF滚动条可调整大小的窗口

c# - 在单个 ContentControl 中管理多个 View / View 模型

c# - 以编程方式添加图像并将它们定位在 WPF Canvas 上

c# - WPF ListView 选择多个 ListView 项

WPF - 在 ListView 中使用 KeyDown 构建按键字符串

c#-4.0 - 并行执行每个有序执行

wpf - 防止选项卡进入 WPF DataGrid