views:

278

answers:

2

Hellow.

I'd like to know if there were a kind of automatic control sizing in WPF.

I mean, a way to auto size element regarding to the user's screen resolution, without having to define it in the code.

thanks.

+2  A: 

First off, WPF uses device indepdendent pixels which means it will scale appropriately with the DPI the user has chosen. That said, I don't think you're asking about DPI here, rather you're asking about scaling with resolution.

If you want your UI to scale to the available space provided by the Window you can use a ViewBox control as the root element of your Window. Then, everything you put inside there will scale according to the way you configure the Viewbox.

Keep in mind that this isn't the typical approach used by applications as normally they will grow/reflow themselves to make use of more space. For example a Grid may be used to give a more important section of the UI space over another.

Drew Marsh
A: 

The answer is YES. If you want your controls to adjust their size based on the size of the window, there are many ways to do it.

For almost any control these are useful:

HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="..." (set your margin to control the position)

For flexible layout using a grid:

<Grid>
ColumnDefinition.Width="*"
RowDefinition.Height="*"

For central areas surrounded by panels, or filling the rest of a row / column:

<DockPanel>

For keeping something the same shape but stretching or shrinking it to fit:

<ViewBox>

There are many others.

If you want your controls to get bigger or smaller on the screen based on a user's Windows display settings such as DPI, it will happen by default. If the user makes system icons and fonts bigger, your text, icons, fonts, etc will be bigger as well.

If you want to tie the size of your objects on the screen to specific system settings, you can bind to the properties in the SystemParameters and SystemFonts classes, such as:

SystemFonts.CaptionFontSize
SystemFonts.MenuFontSize
SystemFonts.IconFontSize
SystemFonts.StatusFontSize

SystemParameters.IconHeight
SystemParameters.IconVerticalSpacing
SystemParameters.MaximizedPrimaryScreenHeight

there are about 75 of these settings. Normally you would not use these because the normal adjustment WPF makes to your object sizes based on DPI is usually enough.

Normally you wouldn't want to force your on-screen objects to be a particular number of pixels, because they would appear tiny on high-resolution screens even if the end-user has Windows configured for things to appear large. This would not be a good thing. The one exception here is if you want to capture screenshots as bitmaps for a specific resolution of screen. In that case, you can set your size as follows:

control.Height =
  desiredHeightInActualScreenPixels
    * System.Windows.SystemParameters.MenuHeight
    / System.Windows.Forms.SystemInformation.MenuHeight;
Ray Burns