title: Binding object to UserControl
body: <p>I'm trying to control the state of child UserControls from my main UserControl. I was thinking of achieving that by creating a DependencyProperty object in Parent UC and referencing it in XAML when creating Children UCs. </p>
  
  <p>The problem is, I get NullReferenceException in Step.xaml.cs (Child code behind) with everything referencing Controller property, first being StepEnabled property. When I leave out the XAML binding and let it instantiate in constructor, it's ok, though I can't of course control anything, but the way it is Controller property always goes null... Code follows:</p>
  
  <p>ParentViewModel:</p>
  
  <pre><code>public class ParentVM : NotifyPropChanged
  {
      ...
  
      public StepController Step1 = new StepController();
      public StepController Step2 = new StepController();
      public StepController Step3 = new StepController();
  
      public ParentVM()
      {
          ...
  
          GetReady();
      }
  
      public void GetReady()
      {
          Step1.Clear();
          Step2.Clear();
          Step3.Clear();
  
      }
  </code></pre>
  
  <p>StepController:</p>
  
  <pre><code>public class StepController
  {
      public bool StepEnabled;
      public bool? WasSuccessful;
  
      public StepController()
      {
          Clear();
      }
      public void Clear()
      {
          StepEnabled = false;
          WasSuccessful = null;
      }
  }
  </code></pre>
  
  <p>Child (code behind):</p>
  
  <pre><code>public partial class Step : UserControl
  {
      public static DependencyProperty ControllerProperty =
      DependencyProperty.Register("Controller", typeof(StepController), typeof(Step), new UIPropertyMetadata(null));
  
      public StepController Controller
      {
          get { return (StepController)GetValue(ControllerProperty); }
          set { SetValue(ControllerProperty, value); }
      }
  
      ...
  
      public bool StepEnabled
      {
          get { return (bool)Controller.StepEnabled; }
          set { Controller.StepEnabled = value; }
      }
      public bool IsDone
      {
          get
          {
              return (Controller.WasSuccessful != null);
          }
      }
      public bool? WasSuccessful
      {
          get { return Controller.WasSuccessful; }
          set { Controller.WasSuccessful = value; }
      }
  
      public Step()
      {
          InitializeComponent();
          this.DataContext = this;
  
          ...
  
          Controller = new StepController();
  
      }
  
  }
  </code></pre>
  
  <p>ParentUC:</p>
  
  <pre><code>...
  &lt;local:Step Controller="{Binding Path=Step1, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/&gt;
  ...
  </code></pre>
  
  <p>ChildUC:</p>
  
  <pre><code>e.g.:
  &lt;TextBlock IsEnabled="{Binding StepEnabled, UpdateSourceTrigger=PropertyChanged}" /&gt;
  </code></pre>
  
  <p>I'm pretty new to WPF and am still unsure about whole lot of things like DataContext and such, so I guess I'm missing something obvious... Thanks for the help in advance!</p>