#23 – WPF Units

In Windows Forms, control sizes are specified using pixels.  In WPF,  sizes are specified using WPF Units.

1 WPF unit = 1/96 inch.  This means at 96 dpi (typical), 1 WPF Unit = 1 pixel.

But this means that at 120 dpi, 1 WPF unit = 1.25 pixels.  (120/96)

Because everything in a WPF GUI using WPF units for sizing, all controls are properly resized based on the system DPI.  The reason for this is so that they can appear to be the same physical size on a device that happens to have a higher pixel density (DPI).  A 96 unit button will be 1″ wide on a 96 dpi device and 1″ wide on a 120 dpi device (because it’s scaled up to 120 pixels).

This same scaling could be done in Windows Forms using a form’s AutoScaleMode property.  But in WPF, it’s automatic.

The full formula:

# pixels = (# WPF Units) * (DPI / 96)

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

7 Responses to #23 – WPF Units

  1. Pingback: #203 – Window Size and Location Are Specified in Device Independent Units « 2,000 Things You Should Know About WPF

  2. Pingback: #323 – Provide Space Around StackPanel Children Using Margin « 2,000 Things You Should Know About WPF

  3. Pingback: #384 – The Benefits of Flow-Based Layout « 2,000 Things You Should Know About WPF

  4. Pingback: #672 – Mouse Coordinates Are in Device Independent Units « 2,000 Things You Should Know About WPF

  5. Pingback: #759 – Device-Independent Units (DIPs) « 2,000 Things You Should Know About WPF

  6. akramhassan says:

    In the given example, at 120 DP, 1 WPF unit is 1.25 Pixels.
    so (#WPF) = (DPI/96) #Pixels.
    and hence
    #Pixels = (96/DPI) #WPF which is the inverse of the given formula.

    what is missing?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: