Question: Hi i'm new to WPF entirely, I would like to be able to control the margin value of anything that is in the XAML via a variable in the CS file , i've read up a few questions/answers on stackoverflow, tried to implement it as such but it dosen't seem to work . 

This is what i've tried so far, but i couldn't get it to work somehow, really need advise on this.

```
public partial class MainWindow : Window, INotifyPropertyChanged
{
   private Thickness _Margin = new Thickness(100, 20, 0, 0);

   public Thickness Margin
   {
       get { return _Margin; }
       set
       {
           _Margin = value;
           //Notify the binding that the value has changed.
           this.OnPropertyChanged("Margin");
       }
   }
    public MainWindow()
    {
        InitializeComponent();

        No.DataContext = _Margin;
    }

    protected void OnPropertyChanged(string strPropertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

```

XAML:

```
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
<Grid Name="No">
    <Border BorderBrush="Silver" BorderThickness="1" Height="100" HorizontalAlignment="Left" Margin="{Binding _Margin}" Name="border1" VerticalAlignment="Top" Width="200" />
</Grid>
</Window>

```

Answer: 
There is a problem with the property name "public Thickness Margin" you use it is overrides the Window.Margin property you should rename it. Second you are binding the member value not the property value.

Try this code (I added a button to trigger the PropertyChanged event you should replace it whatever you want to use):

```
public partial class MainWindow : Window, INotifyPropertyChanged
{
    private Thickness _margin = new Thickness(100, 20, 0, 0);

    public Thickness GridMargin
    {
        get { return _margin; }
        set
        {
            _margin = value;
            //Notify the binding that the value has changed.
            this.OnPropertyChanged("GridMargin");
        }
    }

    public MainWindow()
    {
        InitializeComponent();

        No.DataContext = this;
    }

    protected void OnPropertyChanged(string strPropertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        Random r = new Random();
        GridMargin = new Thickness(r.Next(0, 100));
    }
}

```

XAML:

```
<Grid Name="No">
    <Border BorderBrush="Silver" BorderThickness="1" Height="100" HorizontalAlignment="Left" Margin="{Binding GridMargin}" Name="border1" VerticalAlignment="Top" Width="200" />

    <Button Content="Margin" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="ButtonBase_OnClick"></Button>
</Grid>

```
There's an inconsistency in how you are setting the data context. The `DataContext` is being set to the "\_Margin" property of "MainWindow", but then the binding is also targeting the path "\_Margin". You need this instead:

```
No.DataContext = this;

```