【问题标题】:Automatic tooltip when text is trimmed in DataGrid在 DataGrid 中修剪文本时的自动工具提示
【发布时间】:2014-05-07 17:32:34
【问题描述】:

我的应用程序使用 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 单元格上,而它适用于孤立的文本块。

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

方案一的风格:

<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>

谢谢

【问题讨论】:

    标签: c# wpf datagrid tooltip trim


    【解决方案1】:

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

    我输入的样式&lt;DataGrid.Resources&gt;

    <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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      我还不能发表评论,我缺少所需的积分。

      但我只是想在接受的答案代码中添加一个额外的提示,供像我这样想要使用它的菜鸟使用。

      如果您想将您的转换器链接到它的代码,请确保在您的窗口/用户控件资源中创建一个链接,如下所述:

      <Window.Resources>
          <local:TrimmedTextBlockVisibilityConverter x:Key="TrimToVisConverter" />
      </Window.Resources>
      

      关于已接受答案的其他想法:您还必须将默认 DataGridCell 模板附带的任何样式重写为 DataGridCell States。否则你会失去对行选择和其他东西的清晰视图......

      【讨论】:

        猜你喜欢
        • 2011-09-14
        • 1970-01-01
        • 2018-04-19
        • 2021-02-04
        • 1970-01-01
        • 1970-01-01
        • 2015-09-16
        • 2014-12-10
        • 1970-01-01
        相关资源
        最近更新 更多