#23 – WPF Units
August 4, 2010 7 Comments
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)
Pingback: #203 – Window Size and Location Are Specified in Device Independent Units « 2,000 Things You Should Know About WPF
Pingback: #323 – Provide Space Around StackPanel Children Using Margin « 2,000 Things You Should Know About WPF
Pingback: #384 – The Benefits of Flow-Based Layout « 2,000 Things You Should Know About WPF
Pingback: #672 – Mouse Coordinates Are in Device Independent Units « 2,000 Things You Should Know About WPF
Pingback: #759 – Device-Independent Units (DIPs) « 2,000 Things You Should Know About WPF
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?
I’m not sure what you’re asking here. The correct formula is: NumPixels = NumWPFUnits * (DPI / 96)