在DataGrid中修剪文本时的自动工具提示 [英] Automatic tooltip when text is trimmed in DataGrid

查看:72
本文介绍了在DataGrid中修剪文本时的自动工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My application runs with C# and WPF (.net framework 4.0). My goal is to have a DataGrid in which the text in the cells is trimmed with an ellipsis, and automatically has a tooltip with the full text displayed only if the text in the cell is actually trimmed.

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

Solution 1: I'm currently using this to know if the text is trimmed or not: http://tranxcoder.wordpress.com/2008/10/12/customizing-lookful-wpf-controls-take-2/ The problem is that it only works when I resize the columns. The tooltips don't show up when the DataGrid is first loaded, when the columns are sorted, or when the ItemSource of the DataGrid is updated.

解决方案2 :我也尝试过此操作: http://www.scottlogic.com/blog/2011/01/31/automatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf.html
但是工具提示永远不会出现在我的DataGrid单元中,尽管它可以与孤立的文本块一起正常工作。

Solution 2: I've also tried this:http://www.scottlogic.com/blog/2011/01/31/automatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf.html But the tooltips never appear on my DataGrid cells, while it works fine with isolated textblocks.

我正在寻找改善解决方案1的简单方法并使其在所有情况下都可以在我的DataGrid中工作,或者使用其他方法。

I'm looking for simple ways to improve Solution 1 and make it work in my DataGrid in all cases, or maybe a different approach.

解决方案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>

TextBlockService的源代码

解决方案1的数据网格:

The DataGrid for Solution 1:

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

谢谢

推荐答案

我已经找到了基于 xr280xr的答案
在任何情况下都可以直接使用,而无需使用其他代码。

I've found the perfect solution, based on an answer by xr280xr. It works out of the box, in any condition, and without using additional code.

我放入的样式; 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();
    }
}

这篇关于在DataGrid中修剪文本时的自动工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆