user2088807
user2088807

Reputation: 1408

Text Trimming on DataGrid Template Column

I have the following Column in my datagrid :

<DataGridTemplateColumn  CanUserReorder="False" CanUserResize="True" Header="">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate />
                    </DataGridTemplateColumn.CellEditingTemplate>
                    <DataGridTemplateColumn.CellStyle>
                        <Style TargetType="DataGridCell" BasedOn="{StaticResource DatagridCellHyperlinkStyle}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Border Padding="{TemplateBinding Padding}"  VerticalAlignment="Center">
                                            <TextBlock Width="Auto" Height="Auto" TextTrimming="CharacterEllipsis">
                                        <Hyperlink>
                                         <InlineUIContainer TextDecorations="{Binding Path=TextDecorations, RelativeSource={RelativeSource AncestorType=TextBlock}}" Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=TextBlock}}">
                                          <ContentPresenter Width="Auto" Height="Auto" Content="{Binding DataContext.Value, RelativeSource={RelativeSource AncestorType=DataGridRow}}"/>
                                         </InlineUIContainer>
                                            <Hyperlink.Style>
                                                <Style TargetType="Hyperlink" BasedOn="{StaticResource HyperlinkStyle}">
                                                                <EventSetter Event="Hyperlink.Click" Handler="Click" />

                                                    </Style>
                                            </Hyperlink.Style>
                                        </Hyperlink>
                                            </TextBlock>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGridTemplateColumn.CellStyle>
                </DataGridTemplateColumn>

The hyperlink works perfectly (with my style also) but the text trimming doesn't work. How can I change my code to make it work ?

The 2 styles attached :

<Style x:Key="DatagridCellHyperlinkStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="Padding" Value="5" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="FontSize" Value="14" />
    <Setter Property="FontFamily" Value="Helvetica" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Foreground" Value="{StaticResource CouleurBouton}"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{StaticResource ResourceKey=CouleurBouton}"/>
            <Setter Property="Foreground" Value="{StaticResource ResourceKey=CouleurFond}" />
        </Trigger>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{StaticResource ResourceKey=CouleurBouton}"/>
            <Setter Property="Foreground" Value="{StaticResource ResourceKey=CouleurFond}" />
        </Trigger>
        <DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True">
            <Setter Property="Background" Value="{StaticResource ResourceKey=CouleurBoutonHover}"/>
            <Setter Property="Foreground" Value="{StaticResource ResourceKey=CouleurTexteBoutonHover}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

<Style x:Key="HyperlinkStyle" TargetType="{x:Type Hyperlink}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="{DynamicResource CouleurBoutonPressed}" />
        </Trigger>
    </Style.Triggers>
    <Setter Property="Foreground" Value="{DynamicResource CouleurBouton}" />
    <Setter Property="TextBlock.TextDecorations" Value="{x:Null}" />
</Style>

Thank you !

Upvotes: 0

Views: 1278

Answers (2)

Eugene Podskal
Eugene Podskal

Reputation: 10401

Well, for WPF engine to understand that the trimming is needed it should see that the control cannot be put into the space available. If the control can be resized(AutoSize) it will just increase its dimensions without any trimming.

From MSDN:

Gets or sets the text trimming behavior to employ when content overflows the content area.

And I can't see anything in your template that suggests that the space limit will be encountered.

So try to set width limit, either on Column, or on the TextBlock. Or restrict the resize in some other way.

<TextBlock Width="Auto" Height="Auto" 
           MaxWidth="100"
           MinWidth="30"
           TextTrimming="CharacterEllipsis">

Upvotes: 1

Sheridan
Sheridan

Reputation: 69959

You have nothing that will restrict the TextBlock.Width, so the text in it will never wrap, or be trimmed. To fix this problem, you just need to set some kind of Width restriction on it... you could try something like this:

<ControlTemplate>
    <Border Padding="{TemplateBinding Padding}"  VerticalAlignment="Center">
        <TextBlock MaxWidth="250" TextTrimming="CharacterEllipsis">
            ...

Upvotes: 1

Related Questions