tags:

views:

997

answers:

3

How do you do the equivalent of css's margin-top in WPF?

I have an image which I want to add a margin on the top, but all I can seem to get to work is margin, which effects each side of the image.

+3  A: 

You can specify the margin for each side (in that order : left, top, right, bottom)

<Image Source="image.png" Margin="0,10,0,0"/>
Thomas Levesque
Thanks for the quick replies. I only tried Margin="10 0" thinking that would set the top and bottom to 10 and the sides to 0.I should have figured this one out on my own. Thanks!
djschwartz
WPF will let you do Margin="10,0" but the first number is the sides, the second is top/bottom.
Bryan Anderson
A: 

You could use the left, top, right, bottom numbers to specify the margin for any control in WPF

For example:

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

Here Left -> 10, Top -> 5, Right - 10 and Bottom -> 10

For more check this blog post WPF Margin demystified

Another useful blog post about Margins, Padding, Borders and Content, nice one

Vin
A: 

the Margin property is what you are looking for. There are 3 different ways to set the margin. The first one (see below) sets all of the margins to the same value--it expands out to "0,0,0,0". the second one sets the left and right sides to 1 and the top and bottom sides to 0--it expands out to "1,0,1,0". and the third sets each side to an individual value (in this case, 5). Margin values, in order:first value is left side second value is top
third value is right side fourth value is bottom

Margin="5";      <!-- same as "5,5,5,5" -->
Margin="5,2"     <!-- same as "5,2,5,2" -->
Margin="5,6,7,8" <!-- set left,top,right,bottom independantly -->
Muad'Dib
MS documentation on FrameworkElement.Margin indicates that Margin="#,#" is used for specifying symetrical margins. So "5,2" is the same as "2.5,1,2.5,1"
Josh G
hmm, doesn't seem to work that way when in any of my code--could just be me, tho.
Muad'Dib
5,2,5,2 is symmetrical as well and is how it actually works in practice.
Bryan Anderson