Setting TextTrimming (CharacterEllipsis) in the DataGrid Cell

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

DataGrid cells without TextTrimming set

I applied my own DataGridCell template, as in this answer (code below), and it works well, except for the hyperlink columns such as the first in the picture), which are now empty.

TextTrimming set on text columns, but hyperling column contents missing

<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 see the difference in both types of columns in the visual tree: Datagrid row in visual tree (when no custom template is applied)

but I don’t understand how I can use this information to apply TextTrimming columns to both types of TextBlocks. Thank you for your time;)

+5
source share
1 answer

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

1) x: CellStyle DataGridTextColumns, , 't fit

2) DataGridHyperlinkColumns, App.xaml :

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

TextBlocks ( CodeNaked answer). , , , .

+6

All Articles