c# - 在 DataGrid 中修剪文本时的自动工具提示

标签 c# wpf datagrid tooltip trim

我的应用程序使用 C# 和 WPF (.net framework 4.0) 运行。我的目标是拥有一个 DataGrid,其中单元格中的文本用省略号修剪,并且只有在单元格中的文本实际被修剪时才会自动显示包含完整文本的工具提示。

解决方案 1:我目前正在使用它来了解文本是否被修剪:http://tranxcoder.wordpress.com/2008/10/12/customizing-lookful-wpf-controls-take-2/ 问题是它仅在我调整列大小时有效。首次加载 DataGrid、对列进行排序或更新 DataGrid 的 ItemSource 时,不会显示工具提示。

解决方案 2:我也试过这个:http://www.scottlogic.com/blog/2011/01/31/automatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf.html 但是工具提示从未出现在我的 DataGrid 单元格上,但它可以很好地处理孤立的文本 block 。

我正在寻找改进解决方案 1 并使其在所有情况下都能在我的 DataGrid 中工作的简单方法,或者寻找不同的方法。

解决方案 1 的样式:

<UserControl.Resources>
    <Style x:Key="TextColumnElementStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockService}">
        <Style.Setters>
            <Setter Property="TextWrapping" Value="NoWrap" />
            <Setter Property="TextTrimming" Value="WordEllipsis" />
        </Style.Setters>
    </Style>
</UserControl.Resources>

The source code of the TextBlockService

解决方案 1 的 DataGrid:

<DataGrid ItemsSource="{Binding IssueList}" tbs:TextBlockService.AutomaticToolTipEnabled="True">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Description" Binding="{Binding Description}" 
            ElementStyle="{StaticResource TextColumnElementStyle}">
    </DataGrid.Columns>
</DataGrid>

谢谢

最佳答案

我找到了完美的解决方案,基于 an answer by xr280xr . 它开箱即用,适用于任何条件,无需使用额外代码。

样式,我放在 <DataGrid.Resources> 中:

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}" TextTrimming="CharacterEllipsis">
                    <TextBlock.ToolTip>
                        <ToolTip Visibility="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget, Converter={StaticResource TrimToVisConverter}}">
                            <ToolTip.Content>
                                <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}"/>
                            </ToolTip.Content>
                        </ToolTip>
                    </TextBlock.ToolTip>
                 </TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

来源Converter={StaticResource TrimToVisConverter} :

public class TrimmedTextBlockVisibilityConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return Visibility.Collapsed;

        FrameworkElement textBlock = (FrameworkElement)value;

        textBlock.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));

        if (((FrameworkElement)value).ActualWidth < ((FrameworkElement)value).DesiredSize.Width)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

关于c# - 在 DataGrid 中修剪文本时的自动工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22709715/

相关文章:

c# - 重置DataGrid排序回到其初始状态

wpf - 如何使用 WPF Toolkit Datagrid 更改单元格的背景颜色

c# - 将值(int)设置为检查单选按钮 wpf

wpf - 实心画笔属性不绑定(bind)

c# - 异步 ICommand 实现

c# - 使用 Parallel.ForEach() 时防止远程系统过载

c# - 视频不使用统一在 Visual Studio 的本地机器上播放

c# - 可变结构与类?

c# - 如何在使用 IDataErrorInfo 时检查模型是否有效或有错误

c# - 在鼠标悬停时更改矩形背景