title: Data Binding in WPF User Controls
body: <p>I am creating a UserControl for a series of controls shared by several windows. One of the controls is a Label which shows the flow of some other process in terms of "protocol numbers".</p>
  
  <p>I am trying to offer DataBinding with this Label so the Window automatically reflects the state of the process as the protocol number variable changes.</p>
  
  <p>This is the User control XAML:</p>
  
  <pre><code>&lt;UserControl Name="MainOptionsPanel"
      x:Class="ExperienceMainControls.MainControls"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      DataContext="{Binding RelativeSource={RelativeSource Self}}"
      &gt;
  &lt;Label Height="Auto" Name="numberLabel"&gt;Protocol:&lt;/Label&gt;
  &lt;Label Content="{Binding Path=ProtocolNumber}" Name="protocolNumberLabel"/&gt;
  (...)
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>And this is the Code-Behind:</p>
  
  <pre><code>public partial class MainControls 
  {
      public MainControls()
      {
          InitializeComponent();
      }
  
      public int ProtocolNumber
      {
          get { return (int)GetValue(ProtocolNumberProperty); }
          set { SetValue(ProtocolNumberProperty, value); }
      }
  
      public static DependencyProperty ProtocolNumberProperty = 
         DependencyProperty.Register("ProtocolNumber", typeof(int), typeof(MainControls));
  }
  </code></pre>
  
  <p>This seems to be working because if on the constructor I set ProtocolNumber to an arbitrary value, it is reflected in the user control.</p>
  
  <p>However, when using this usercontrol on the final window, the data binding breaks.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="UserControlTesting.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:expControl="clr-namespace:ExperienceMainControls;assembly=ExperienceMainControls"
      DataContext="{Binding RelativeSource={RelativeSource Self}}"
      &gt;
      &lt;StackPanel&gt;
          &lt;expControl:MainControls ProtocolNumber="{Binding Path=Number, Mode=TwoWay}" /&gt;
      &lt;/StackPanel&gt;
  
  &lt;/Window&gt;
  </code></pre>
  
  <p>Code-Behind for window:</p>
  
  <pre><code>public partial class Window1 : Window
  {
      public Window1()
      {
          Number= 15;
          InitializeComponent();
      }
  
      public int Number { get; set; }
  }
  </code></pre>
  
  <p>This sets the Protocol Number to zero, ignoring the value set to Number.</p>
  
  <p>I've read example</p>