Home > XAML > Yet another reason to love WPF.

Yet another reason to love WPF.

November 6th, 2009 Casey Leave a comment Go to comments
VN:R_U [1.6.3_896]
Rating: 0.0/5 (0 votes cast)

While reviewing some XAML a few days ago, I ran across this:

<Button Margin="1, 1, 1, 1" ... />

This XAML respectively sets the left, top, right and bottom margins of  the button element to 1.  While this technically works, WPF provides a handy shortcut for setting all margins to the same value:

<Button Margin="1" ... />

If you only provide one value, WPF will assume that you want to set all margins to 1.  Check this out:

<Button Margin="1, 2" ... />

This time, we’re setting the left and right margins to 1 and the top and bottom margins to 2.

I rarely find myself needing to set different values for the left, top, right and bottom margins for a given user interface element in WPF.  If you only need to set one margin, you still need to use the original syntax.  For example, if I was setting only the left margin to 1, I would have to do something like this:

<Button Margin="1, 0, 0, 0" ... />

Note that these syntax rules apply to the Padding property as well.  After wrestling with CSS for years, the layout model in WPF is a welcome breath of fresh air.

VN:R_U [1.6.3_896]
Rating: 0.0/5 (0 votes cast)

Categories: XAML Tags:
  1. November 6th, 2009 at 13:42 | #1

    CSS respects the same shorthand notation outlined here. Except that WPF decided to go with “left, top, right, bottom” instead of “top, right, bottom, left” used by CSS :( .

    VN:F [1.6.3_896]
    Rating: 0.0/5 (0 votes cast)
  2. November 6th, 2009 at 14:32 | #2

    I was not aware of that. In general, though, my experiences with layout in WPF have been much easier than those with CSS.

    VN:F [1.6.3_896]
    Rating: 0.0/5 (0 votes cast)
  1. No trackbacks yet.