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.
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.
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 .
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.
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.
You can specify the margin for each side (in that order : left, top, right, bottom)
<Image Source="image.png" Margin="0,10,0,0"/>
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 -->
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