0
votes

I'm trying to auto scale the font and wrap text in a TextBlock in WPF and I can't figure it out.

I've Googled it and looked at stackoverflow loads of times and the main suggestion is to place a TextBlock inside a ViewBox. I've tried that, and all it does is scale the whole text down to one line instead of wrapping it.

If I just use a TextBlock without a ViewBox it wraps, but doesn't scale to fit. It's driving me mad, as I am literally trying to move from WinForms to WPF to make better looking UIs.

I've tried StackPanel and DockPanel and they still don't have the desired effect.

All I want is a TextBlock to take a string of text of unknown size and display it scaled and wrapped. I don't understand why it's so difficult

1
Can you post the code that you've already tried? especially what you mean by scale the whole text down to one lineAniruddha Varma
Do you have TextWrapping="Wrap" in your TextBlock? What is the ViewBox Stretch property set to? And the TextBlock Width and Height? Note that these don't have the same meaning as usual in this context.Jimi
I have TextWrapping="Wrap" (I've tried changing it to WrapWithOverflow as well). I've played with the Stretch property of the ViewBox, trying "Fill", "Uniform", "UniformToFill". I've tried the changing the width and height as well. Like I said, the ViewBox just makes the text small until it all fits on one line. And the wrapping of the TextBlock doesn't scale the font size in wrapping,Wilko84
You can't do this with the built-in controls, at least not in a way that works consistently across control size. See this: stackoverflow.com/q/39792867/4265041Steven Rands

1 Answers

0
votes

It is helpful to include code of what you have tried.

When I do this:

<Grid>
    <TextBox VerticalAlignment="Center" TextWrapping="Wrap"  Width="100"/>
</Grid>

I get this:

WPF textbox with wrapping

Is that what you are looking for?

You could also check out the RichTextBox if you need more features.