TextTrimming from left

Boris picture Boris · Jan 19, 2012 · Viewed 8.8k times · Source

Is there a way to specify text trimming on a TextBlock to be from the left side?

I've manage to accomplish two out of three scenarios (the third being the one I need):

  1. Regular trimming

    <TextBlock 
        VerticalAlignment="Center" 
        Width="80" 
        TextTrimming="WordEllipsis"
        Text="A very long text that requires trimming" />
    
    // Result: "A very long te..."
    
  2. Left trimming

    <TextBlock 
        VerticalAlignment="Center" 
        Width="80" 
        FlowDirection="RightToLeft"
        TextTrimming="WordEllipsis"
        Text="A very long text that requires trimming." />
    
    // Result: "...A very long te"
    
  3. Left trimming where the end of the text is seen

    // Desired result: "...uires trimming"
    

Does anyone know if this is possible? Thanks.

Answer

Jeffrey Harmon picture Jeffrey Harmon · Jan 23, 2012

If you don't care about the ellipses, but just want to see the end of the text instead of the beginning when it gets cut-off, you can wrap the TextBlock inside another container, and set its HorizontalAlignment to Right. This will cut it off just like you want, but without the elipse.

<Grid>
    <TextBlock Text="Really long text to cutoff." HorizontalAlignment="Right"/>
</Grid>