wpf 在 DataGrid 的单元格中设置 TextTrimming (CharacterEllipsis)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11850217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:53:20  来源:igfitidea点击:

Setting TextTrimming (CharacterEllipsis) in DataGrid's Cell

wpfwpfdatagrid

提问by Pawe? Bulwan

I would like to apply TextTrimming property (CharacterEllipsis) to the text in WPF DataGrid cells.

我想将 TextTrimming 属性(CharacterEllipsis)应用于 WPF DataGrid 单元格中的文本。

DataGrid cells without TextTrimming set

未设置 TextTrimming 的 DataGrid 单元格

I applied custom DataGridCell template as in this answer(code below) and it works well, except for the Hyperlink columns like the first one in the picture), which now are empty.

我在这个答案(下面的代码)中应用了自定义 DataGridCell 模板,它运行良好,除了像图片中第一个这样的超链接列),它们现在是空的。

TextTrimming set on text columns, but hyperling column contents missing

在文本列上设置了 TextTrimming,但缺少超级列内容

<Style TargetType="DataGridCell">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border Padding="3" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                        <ContentPresenter.ContentTemplate>
                            <DataTemplate>
                                <TextBlock TextTrimming="CharacterEllipsis" Text="{Binding Text}"/>
                            </DataTemplate>
                        </ContentPresenter.ContentTemplate>
                    </ContentPresenter>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I can see the difference in both column types in visual tree: Datagrid row in visual tree (when no custom template is applied)

我可以在可视化树中看到两种列类型的差异: 可视化树中的 Datagrid 行(未应用自定义模板时)

but don't understand how I can use this information to apply TextTrimming to TextBlock's columns of both type. Thanks for your time ;)

但不明白我如何使用此信息将 TextTrimming 应用于两种类型的 TextBlock 列。谢谢你的时间 ;)

回答by Pawe? Bulwan

I finally ended up with the following solution (more like a workaround, but it works fine):

我最终得到了以下解决方案(更像是一种解决方法,但效果很好):

1) I assigned an x:Keyto the style in question and applied it as a CellStyleto all DataGridTextColumns that should have their contents trimmed and ellipsisized whenever they don't fit

1) 我为有问题的样式分配了一个x:Key并将其作为CellStyle应用于所有 DataGridTextColumns,只要它们不适合,就应该修剪和省略其内容

2) To apply ellipsis trimming in DataGridHyperlinkColumns, in App.xamlI added the following style:

2) 要在 DataGridHyperlinkColumns 中应用省略号修剪,在App.xaml 中我添加了以下样式:

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="TextTrimming" Value="CharacterEllipsis"></Setter>
</Style>

which will apply to all implicitly generated TextBlocks (as described in CodeNaked's answer). This might seem a bit overkill, but I can't see much difference in rendering performance and Hyperlinks are now trimmed as expected.

这将适用于所有隐式生成的 TextBlocks(如CodeNaked 的回答中所述)。这似乎有点矫枉过正,但我​​看不出渲染性能有太大差异,现在超链接已按预期修剪。