Friday, 10 May 2013

Alignment-Margin-Padding | WPF Tutorial pdf

Alignment, Margin and padding are the 3 most important properties that you should always consider for every UIElement. Before going further with the containers, you need to know about them.

Alignment : Alignment determines how the child elements will be placed within the allocated space of the Parent Element. In other words it determines the position on
the space it was provided.
There are two types of Alignment:
1. HorizontalAlignment :It has 4 possible values Left, Right, Center and Stretch. Stretch is the Default value of any HorizontalAlignment.
2. VerticalAlignment : It has 4 possible Values Top, Center, Bottom and Stretch.
Stretch is the default valueof any VerticalAlignment.

Margin : It determines the distance between one control to the boundary of the cell where it is placed. It can be uniform when specified as a integer value, or you can use TypeConverter to specify the value of all its sides. For instance :
Margin = "20" means Left=20, Top=20, Right=20 and Bottom=20. You can also specify as
Margin="20,10,0,10" which means Left =20, Top=10, Right=0, and Bottom=10.
<Button Margin="0,10,0,10">Button 1</Button>
<Button Margin="0,10,0,10">Button 2</Button>
<Button Margin="0,10,0,10">Button 3</Button>
Padding: Padding is present in few of the controls that helps in enlarging the size of the control by its value. So it is almost similar, but Margin places space outside the boundary of the control whereas padding places it inside the boundary of the control.
<Button Padding="0,10,0,10">Button 1</Button>
<Button Padding="0,10,0,10">Button 2</Button>
<Button Padding="0,10,0,10">Button 3</Button>
Each of the Margin and padding takes an object of Thickness.
Button bb = new Button();
bb.Margin = new Thickness(20);
bb.Padding = new Thickness(10, 20, 30, 10);
this.MyGrid.Children.Add(bb);

No comments: