0
votes

I am an experienced developer who is new to WPF. I appreciate all the flexibility of WPF components, but this flexibility is causing me a little trouble.

Specifically, I want to create a dialog box. When I do this in C++/MFC or C#/Win Forms, my text boxes and the text box fonts are automatically the right size for system dialogs.

But in WPF, I have to hard code both the text box height and the font size. This apparently means:

  1. I have to guess what font size will best match the user's system font settings for dialog boxes, and my fonts will not adjust to each users' system settings.

  2. Even worse, I have to also guess at the best text box size for the current font. This has always been automatic on other platforms.

I'm just wondering how seasoned WPF developers are handling text box and font sizes within a dialog box.

EDIT:

In other words, I would like my text boxes to use the font returned by the Windows API calls GetStockObject(SYSTEM_FONT). Is this directly supported by WPF?

2

2 Answers

1
votes

I'm having a hard time understanding your question, mainly because WPF works better than either MFC or Winforms for the scenarios you seem to be asking about.

  1. I have to guess what font size will best match the user's system font settings for dialog boxes, and my fonts will not adjust to each users' system settings.

WPF uses device-independent metrics. In particular, WPF pixels are always 96 per nominal inch. Of course, depending on user settings, the actual size of a WPF "inch" may be more or less than an real inch. But those are the settings under control of the user, specifically designed to do such scaling.

One big advantage of WPF's model is that unlike Winforms, the relative layout of everything stays the same regardless of scaling. In Winforms, different things scale differently, causing line-breaks to happen in different places, text to be truncated, etc.

  1. Even worse, I have to also guess at the best text box size for the current font. This has always been automatic on other platforms.

The WPF Window class has a SizeToContent property. If you set this to Both, the window will resize itself to exactly contain whatever content you put in it. Is this not what you want?


If the above does not address your specific scenario, please help us understand your scenario by providing a good, minimal, complete code example that shows clearly the problem you are having, with a precise explanation of what that code does and how that's different from what you want.

1
votes

You can set the size of your Controls to "Auto" like this: <TextBlock Heigh="Auto" Text="Foobar"/>, the control's height will then adapt to the content. This also work for any Height or Width properties on any controls. Hope I helped ;)