6

As background I have a very long ID that too long to display in the given area of the TextBlock. The interesting portion of the ID is the end, that is the rightmost portion.

What I would like to do is have the TextBlock, rather than the text overflowing right and cutting off the rightmost portion, overflow left and cutoff the leftmost portion.

That is given the ID 123456 and a TextBlock with enough space to hold four characters, to get the TextBlock to display 3456 rather than 1234 as it does by default.

I could manually trim my ID for display, but given a variable spaced font that's not ideal. So is there someway to get WPF do change the overflow direction?

4 Answers 4

5

Is this the effect you're trying to get? It sounds like it:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel Width="200">  
      <TextBlock Background="Honeydew" HorizontalAlignment="Right">
        The quick brown fox jumped over the lazy dog's back
      </TextBlock>
    </StackPanel>
</Page>
Sign up to request clarification or add additional context in comments.

Comments

5

You simply need to set the following attribute FlowDirection="RightToLeft" in the XAML for the TextBox

Comments

2

If you would also like to show the full text when a user hovers their mouse over the clipped TextBlock, it's a bit tricky, but there is a technique.

Comments

2

In order to show the entire text when the user hovers over the TextBox, simply bind the ToolTip to the Text property of the TextBox:

ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Text}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.