Search code examples
wpfsilverlightxamltexttrimming

TextTrimming from left


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.


Solution

  • You can't do this out-of-the-box, but I can think of two things that might work:

    1) Create an attached property for TextBlock called something like LeftTrimmingText. Then, you would set this property instead of the Text property. E.g.

      <TextBlock my:TextBlockHelper.LeftTrimmingText="A very long text that requires trimming." />
    

    The attached property would calculate how many characters could actually be displayed, and then set the Text property of the TextBlock accordingly.

    2) Create your own class which wraps a TextBlock, and add your own properties to take care of the required logic.

    I think the first option is easier.