Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF equivalent of margin-top?

Tags:

margin

wpf

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.

like image 880
djschwartz Avatar asked May 11 '09 19:05

djschwartz


People also ask

What is margin WPF?

wpf. The margin property in WPF allows you set spacing between elements in a window. You can assign the margins for the Top, Right, Bottom and Left. Every control that derives from FrameworkElement has this property.

How to change margin in c# WPF?

Margin is set as thickness, so the solution could be: test. Margin = new Thickness(0,-5,0,0); Note: Thickness have 4 parameters viz left, top, right and bottom .

What is padding in WPF?

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.

How margin works in XAML?

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.


2 Answers

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

<Image Source="image.png" Margin="0,10,0,0"/>
like image 60
Thomas Levesque Avatar answered Nov 23 '22 15:11

Thomas Levesque


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 -->
like image 40
Muad'Dib Avatar answered Nov 23 '22 14:11

Muad'Dib