WPF does not do primitive font-based scaling because it is ... well, primitive. You can see this in your own screenshots.
Here is your screenshot of "WinForms before changing the font." See how much space is between "sitting on the log". and the right edge of the form.
And here is your screenshot of "WinForms, after changing the font." Notice how much less you get after scaling.
If you had not left too much space, your label would have been cut off with a new font. And with some fonts, this will be cut off even if you leave all the extra space. This is what I mean when I say that scaling WinForms is "primitive." WinForms selects a single scale to apply to everything, and this scale is not selected with awareness of your content; it is based on average font statistics, which can and will fall apart after you start talking about specifics.
WPF doesn't bother you with something primitive. This gives you an amazingly powerful layout system where it would be trivial to create a window that scales perfectly. But instead, you choose to cripple this layout system using hard-coded sizes. Stop him.
Hard-coded sizes have two huge problems:
- They do not adapt to different fonts. You have already noticed this.
- They do not adapt to other content. (What happens when you want to make a German version of your application, and the German text does not fit your hard-coded button size?)
Hard-coded sizes simply do not adapt. For anything. You should have used them in WinForms because all WinForms were supported. But WPF gives you the right layout system, so you don't need (and shouldn't) use anything that's rude.
All you need is:
- A
Window
with SizeToContent="WidthAndHeight"
. Thus, the window will exactly match the size of the text and button, regardless of which font or language you use. - Since you have only two interface elements, and one on top of the other, you must put a
StackPanel
inside your Window
. - Inside the
StackPanel
you need:- A
Label
or TextBlock
to display text with text Content
( Label
) or Text
( TextBlock
); and - A
Button
with HorizontalAlignment="Right"
, and the text in Content
.
- Set <
Margin
to StackPanel
, TextBlock
and Button
to touch things to your liking.
What is it. Do not set any other properties on anything, especially not Width
or Height
.
Now, if you change the font, the window and button will still be the right size and will not cut your text. If you localize your application in another language, the window and button will exactly fit the size and will not cut your text. Stop struggling with WPF and it will give you great results.
If you later want to make your layout more advanced, you can consider things like:
- If you want the button to be a little wider (to have more breathing space before and after the text), try playing with
Padding
or set MinWidth
and MinHeight
. (Do not use Width
or Height
if your button contains text. You might want to use them if your button contains only an image, but maybe not even then.) - If you are worried that the font may make the window so large that it no longer fits the userβs screen and you want to enable word wrap, then play with
MaxWidth
and TextWrapping
.
The WPF layout system is surprisingly powerful. Get to know him. Do not fight it using hard-coded layouts, and then complain that your hard-coded layouts suck.