Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF fit text to TextBlock without overflow

Tags:

c#

wpf

xaml

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?

like image 207
Timo Avatar asked Nov 08 '22 12:11

Timo


1 Answers

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

like image 176
reti Avatar answered Nov 15 '22 13:11

reti