Vadim’s Weblog

Never stop learning.

WPF Margin Property

Posted by Vadim on October 5, 2008

I finally decided to spend some time to learn WPF.  One of the thing I learned today is Margin property.

<Button Margin="10 0 10 5" />

Every control that derives from FrameworkElement class has this WPF_Margin property.

I’m not going to describe this property here. However, I want to share with you how I memorized the sequence of margin.  It goes like this: Left, Top, Right, Bottom.

The easiest way for me to remember is to start from Left and then go clock wise: Top, Right, Bottom.

There are two main reasons why I blog and both of them selfish.

First reason is because writing helps me remember and understand better new things I learn.

The second reason is that people who read my blog comment and correct any mistakes I make and that helps me to learn too.

I have a question for WPF gurus.  I understand that if all margins are the same (for example left, top, right, and bottom are equal to 5), it’s possible to assign margin like this:

<Button Margin="5" />

The both assignments with one number and four numbers make sense because Margin is of type Thickness  and this struct has two constructors:

public Thickness(double uniformLength)
public Thickness(double left, double top, double right, double bottom)

Here comes my question: I discovered that it’s possible to assign Margin declaratively with two numbers.

<Button Margin="10 5" />

How is it possible if Thickness doesn’t have a constructor with two parameters?
kick it on DotNetKicks.com

5 Responses to “WPF Margin Property”

  1. Hi there,

    I am by no means a WPF guru, but I am learning as well.

    Just like with LINQ, the compiler does a LOT of magic behind the scenes when churning XAML into something binary. The XAML you create can be quite different from the actual object structure the compiler builds for you. So my guess would be that something tells the compiler that “x y” must be the same as “x y x y”..

    Rune

  2. ilovetocode said

    Hi,

    Rune has it right. Some properties of WPF objects have implicit converters attached to them (based on type). This allows the input string to take several different forms without having to specify a converter in XAML for each use.

    The converter at work here is a ThicknessConverter which parses the input string and attempts to map the values given in the string to one of the Thickness object constructor overloads.

    For more information take a look at http://msdn.microsoft.com/en-us/library/ms752059.aspx#typeconverterenabled

    Sam.

  3. Vadim said

    Rune & Sam,

    Thanks a lot for you help. You’re absolutely right ThnicknessConverter does the job.
    It has internal method FromString that include the code bellow:

    switch (index)
    {
    case 1:
    return new Thickness(numArray[0]);

    case 2:
    return new Thickness(numArray[0], numArray[1], numArray[0], numArray[1]);

    case 4:
    return new Thickness(numArray[0], numArray[1], numArray[2], numArray[3]);
    }

  4. [...] Margin demystified. In my previous post I asked a question about this line of [...]

  5. Jason said

    My question is: why didn’t the WPF programmers use the well known HTML order for this: Top, Right, Bottom, Left?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>