In CSS the margin order is: top right bottom left
In XAML the margin order is: left top right bottom
Is there a reason why the WPF team didn't align this to the CSS norm?
XAML ValuesMargin="20,50" will be interpreted to mean a Thickness with Left and Right set to 20, and Top and Bottom set to 50. The default unit for a Thickness measure is device-independent unit (1/96th inch). You can also specify other units by appending the unit type strings cm , in , or pt to any measure.
The Margin property of UIElement, which is parent class of all WPF controls is used to set the margin of a control. Margin property takes four numbers - Left, Top, Right and Bottom, that is margin to the left top and right bottom. This example sets Margin of a Button control in XAML at design-time. Click Me!
The Padding property represents the distance between an element and its child elements, and is used to separate the control from its own content. Padding values can be specified on layout classes.
Padding represents the distance between the side of the control (which can be the margin) and its content. The content depends on the type of the control. Margin is outside the UI element, while Padding is inside it. Next Recommended Reading WPF: Textblock Vs Label.
I imagine this is because margins in WinForms were declared as left, top, right, bottom. They likely thought it would be more likely to have WinForms developers move on to WPF rather than those from the web.
For reference you can check the constructor for the Padding structure on MSDN (the type used for the Margin property).
They're not aligning to CSS norms - they're aligning with the way that Microsoft has done UI APIs in the past. I can't speak for WinForms, but in MFC coordinates are specified in the order of LTRB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With