WPF app, having trouble setting vertical bounds on a TextBlock
I am building a WPF application that can project song lyrics on second screen stanza by stanza
I am facing a challenge to set the font size on the MonitorPreviewPanel which is TextBlock basically
I implemented a slider that can set font size and the when the textsize is increased and when the width of the text hits the left and right ends the text is getting wrapped
But somehow the vertical bonds are not working, the text is overflowing when the text hits the top and bottom
13 Replies
Can you post some images, so we can visualise what you're saying?
This is how it looks by default - which is expected behavior
This is how it's appearing when I increase font size(overflow on Y axis)
what I am expecting is this- the user should be able to increase font only until the text touches the top and bottom edges
That's not how it works, though. Font size has priority
You might be able to use a ViewBox, either instead of scaling up, or to scale it down once the font size gets too large
If you want to achieve, that the TextBox is shown with the largest view possible, then try to wrap it in a ViewBox control. - FontSize then does not matter anymore directly.
With this, you could also get rid of the Slider control at all.
Like:
<Viewbox>
<TextBox />
<Viewbox>
Would that be a solution for you? - Otherwise, you are in need to kind of also control the Padding of the TextBox dynamically depending on a ratio, which i would not recommend,
yes I tried with viewbox, it is helping with my requirement but there is a dowside
instead of setting fontsize according the available space it is squuezing in the text, because of which the text looks bad
here is a screenshot, I repeated the first line jst for demo
I used Stretch="Fill"
this is how it should actully look like
I set font size manually here
That's your problem.
Stretch
doesn't preserve the aspect ratio. Try Fill
, see https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.stretchI set the mode as 'Fill'
Sorry, didn't get you.
Stretch is the property name correct? And Fill/Uniform are the modes
Oh sorry, it's first thing in the morning here ><
Fill
doesn't preserve the aspect ratio. Try Uniform
This is how it looks when I use Uniform
Using uniform is not showing largest possible text. If the width is longer then it is not utilising the space on top and bottom
@canton7