5

I have a TextBlock that has a fixed size and variable text. I want my text to always fit in the TextBlock (without ellipsis or overflow, but with word wrap). If the text changes, the font size should be adjusted so that the text fits to the bounds of the TextBlock.

I know I could pack the TextBlock into a Viewbox (which I'm doing currently) but than I don't have word wrap.

The image above shows three different TextBlocks (the blue numbers are just for illustration). If I use a Viewbox I get results like in 1 and 2 but I rather want results like in 1 and 3.

So I'm basically trying to combine Viewbox and word wrap. How can this be done?

5
  • You can use the TextWrapping property to get the wrapping behavior. Commented Dec 23, 2017 at 22:29
  • @pstrjds Unfortunately not. If my TextBlock is in a Viewbox (to achive different font sizes) the Textblock has (theoretical) infinite space and thus wrapping doesn't kick in because the text fits in a single line. Commented Dec 23, 2017 at 22:32
  • My misunderstanding. At first read of your question I thought you wanted to get rid of the Viewbox, on a re-read, I see you want to keep it. Commented Dec 23, 2017 at 22:33
  • @pstrjds Not neccessarly. If I get both features (wrapping and maximum font size), I don't need the Viewbox. I just use it to provide the adjustable font size. Commented Dec 23, 2017 at 22:35
  • I think you could get rid of the Viewbox if you combined the wrapping and the answer from this question to detect when the text is being trimmed, and then adjust the font until it is no longer being trimmed - stackoverflow.com/a/25436070/416574 Commented Dec 23, 2017 at 22:42

1 Answer 1

1

If you restrict the viewbox expansion to one dimension, then you can still have your wrap and something close to #3. Try this answer

Sign up to request clarification or add additional context in comments.

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.