WPF Margin

Jose Leon

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.

A simple mark up XAML code with margins is:

<window x:class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="MainWindow" height="242" width="348">


       <button content="Button" horizontalalignment="Left" margin="120,90,0,0" verticalalignment="Top" width="75"></button>



It'll render something like this:

Margin in code

The margin property is of type Thickness so you should be able to set margins in code using the following snippet:

buttonTest.Margin = new Thickness(0, 120, 9, 213);



All posts

  • licensing
  • analytics

Easy .NET Protection

Purchase template