Set Margin of Controls in WPF

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.
  1. <Button Click="OnClick" Margin="10,20,0,0" Name="btn1">  
  2. Click Me!  
  3. </Button>  
This example sets Margin of a Button control in C# code behind at run-time.
  1. void OnClick(object sender, RoutedEventArgs e)  
  2. {  
  3. Thickness marginThickness = btn1.Margin;  
  4. if(marginThickness.Left == 10)  
  5. {  
  6. btn1.Margin = new Thickness(60);  
  7. }  
  8. else  
  9. {  
  10. btn1.Margin = new Thickness(10);  
  11. }  
  12. }