I'm trying to have text in a block truncate itself when the last line of the text would cause the bottom of the text box to slice it horizontally:

icon with text cut off

<DataTemplate x:Key="largeIconTemplate">
        <StackPanel Width="140" Height="180" >
            <Grid Width="120" Height="120" HorizontalAlignment="Center">
                <Image Source="{Binding IconName, Converter={x:Static converters:StringPathToBitmapImageConverter.Instance}}" Margin="6,6,6,9"/>
            </Grid>
            <TextBlock Text="{Binding Description}" TextWrapping="Wrap" TextAlignment="Center" Margin="0,0,0,1" />
        </StackPanel>
    </DataTemplate>

Setting TextTrimming options seems to make no difference. How to I get the text inside the TextBlock to truncate correctly? i.e. "fit as much of the line as possible with centering and wrapping, but truncate the text if it will be rendered as partially cutoff."

有帮助吗?

解决方案

Your DataTemplate is wrong. a StackPanel is not the appropiate container for that.

Use a DockPanel instead:

<DataTemplate x:Key="largeIconTemplate">
    <DockPanel Width="140" Height="180">
        <Image DockPanel.Dock="Top" Margin="6,6,6,9"
               Source="{Binding IconName, Converter={x:Static converters:StringPathToBitmapImageConverter.Instance}}"/>

        <TextBlock Text="{Binding Description}" 
                   TextTrimming="CharacterEllipsis" 
                   TextWrapping="Wrap" 
                   TextAlignment="Center" 
                   Margin="0,0,0,1" />
    </DockPanel>
</DataTemplate>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top