Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why XAML's Margin attribute has four components not two?

Tags:

wpf

xaml

When you see Margin attribute in a XAML file of WPF, it has four components. Why is this? The first two components make sense they are offset from top left window, but what is it for the third and fourth components when we have Width and Height?

<Grid>
        <Button Content="Button" Height="27" HorizontalAlignment="Left" Margin="29,27,0,0" Name="clickButton" VerticalAlignment="Top" Width="86" Click="clickButton_Click" />
        <TextBox Height="27" HorizontalAlignment="Left" Margin="29,90,0,0" Name="textBoxOut" VerticalAlignment="Top" Width="276" />
</Grid>
like image 781
prosseek Avatar asked Jul 13 '11 00:07

prosseek


People also ask

How does margin work 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.

How do I set margins in XAML?

The Margin property of UIElement, which is parent class of all WPF controls is used to set the margin of a control. Margin property takes four numbers - Left, Top, Right and Bottom, that is margin to the left top and right bottom. This example sets Margin of a Button control in XAML at design-time.

How does margin work WPF?

The margin is the space between an element and the parent element or other adjacent element on the same parent element. The margin adds extra space around the outside edges of an element. The Margin property of FrameworkElement represents the margin of an element. It is a type of Thickness structure.

What dimension units should you use to specify the size of margins and padding in a user interface?

Padding and margin In order to ensure consistent use of whitespace throughout the application, given a component could be used in a variety of contexts, it may be best to use rem for margin and padding than em .


2 Answers

Margin always has four components: left, top, right and bottom. Two components are just shorthand when top=bottom and left=right. And one component is shorthand when all four components are the same.

Here is good explanation of Margins and Paddings.

like image 100
Ivan Danilov Avatar answered Sep 20 '22 06:09

Ivan Danilov


The margin can be different on all sides of an element. Therefore the four elements are:

Margin="left,top,right,bottom"

refer here:

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.margin.aspx

like image 45
hitch Avatar answered Sep 20 '22 06:09

hitch