title: WPF Binding Confusion: Composite DependencyObject
body: <p>I have a DependencyObject class composition that looks like the following:</p>
  
  <pre><code>public class A : DependencyObject {
      public AB AB { get { ... } set { ... } }
      public AB AC { get { ... } set { ... } }
  }
  
  public class AB : DependencyObject {
      public string Property1 { get { ... } set { ... } }
      public string Property2 { get { ... } set { ... } }
      public string Property3 { get { ... } set { ... } }
  }
  
  public class AC : DependencyObject {
      public string Property1 { get { ... } set { ... } }
      public string Property2 { get { ... } set { ... } }
  }
  </code></pre>
  
  <p>On A, AB and AC all properties perform the typical GetValue and SetValue operations referencing static properties per usual. </p>
  
  <p>Now, classes A, AB and AC have corresponding UserControls AGroupBox, ABGrid, ACGrid. AGroupBox has a root A class property, ABGrid has a root AB class property and ACGrid has a root AC class property.</p>
  
  <p>Both ABGrid and ACGrid have working bindings (e.g., ABGrid Contains a TextBox control whose Text property is twoway bound to AB's Property1.) I've verified this by creating a simple Window and having ABGrid be Window's only Content child and in the code behind setting ABGrid.AB = new AB(); same scenario for ACGrid.AC = new AC();. </p>
  
  <p>The problem is when I try to do similarlly with with AGroupBox. I try adding AGroupBox as the single child of Window's Content in XAML, and set the AGroupBox.A property to new A() {AB = new AB(), AC = new AC()}; and the binding of the controls fails. AB and AC have default values for their PropertyN properties.</p>
  
  <p>Any insights on what I'm missing? Is there a different route I should be taking?</p>
  
  <p>EDIT: Additional Comment- If I add a string property to A, (String1) and bind it to the Text part of the GroupBox then the binding to that property works, but not to the AC and AB property of A.</p>
  
  <p>EDIT-2: Per David Hay's request (all code is in namespace wpfStackOverflow):</p>
  
  <p><strong>A.cs</strong></p>
  
  <pre><code>public class A : DependencyObject {
      static public DependencyProperty BProperty { get; private set; }
      static public DependencyProperty CProperty { get; private set; }
      static public DependencyProperty PropertyProperty { get; private set; }
  
      static A() {
          BProperty = DependencyProperty.Register("B", typeof(B), typeof(A));
          CProperty = DependencyProperty.Register("C", typeof(C), typeof(A));
          PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(A));
      }
  
      public B B {
          get { return (B)GetValue(BProperty); }
          set { SetValue(BProperty, value); }
      }
  
      public C C {
          get { return (C)GetValue(CProperty); }
          set { SetValue(CProperty, value); }
      }
  
      public string Property {
          get { return (string)GetValue(PropertyProperty); }
          set { SetValue(PropertyProperty, value); }
      }
  
      public A() {
          Property = "A's Default Value";
          B = new B();
          C = new C();
      }
  }
  </code></pre>
  
  <p><strong>B.cs</strong></p>
  
  <pre><code>public class B : DependencyObject {
      static public DependencyProperty PropertyProperty { get; private set; }
  
      static B() {
          PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(B));
      }
  
      public string Property {
          get { return (string)GetValue(PropertyProperty); }
          set { SetValue(PropertyProperty, value); }
      }
  
      public B() {
          Property = "B's Default Value";
      }
  }
  </code></pre>
  
  <p><strong>C.cs</strong></p>
  
  <pre><code>public class C : DependencyObject {
      static public DependencyProperty PropertyProperty { get; private set; }
  
      static C() {
          PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(C));
      }
  
      public string Property {
          get { return (string)GetValue(PropertyProperty); }
          set { SetValue(PropertyProperty, value); }
      }
  
      public C() {
          Property = "C's Default Value";
      }
  }
  </code></pre>
  
  <p><strong>AGroupBox.xaml</strong></p>
  
  <pre><code>&lt;UserControl 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:wpfStackOverflow"
      x:Class="wpfStackOverflow.AGroupBox"
      DataContext="{Binding RelativeSource={RelativeSource Self}, Path=A}"
      Width="300"
      Height="72"
      &gt;
      &lt;GroupBox Header="{Binding Property}"&gt;
          &lt;StackPanel &gt;
              &lt;local:BGrid B="{Binding B}"/&gt;
              &lt;local:CGrid C="{Binding C}"/&gt;
          &lt;/StackPanel&gt;
      &lt;/GroupBox&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>AGroupBox.xaml.cs</strong></p>
  
  <pre><code>public partial class AGroupBox : UserControl {
      static public DependencyProperty AProperty { get; private set; }
  
      static AGroupBox() {
          AProperty = DependencyProperty.Register("A", typeof(A), typeof(AGroupBox));
      }
  
      public A A {
          get { return (A)GetValue(AProperty); }
          set { SetValue(AProperty, value); }
      }
  
      public AGroupBox() {
          InitializeComponent();
      }
  }
  </code></pre>
  
  <p><strong>BGrid.xaml</strong></p>
  
  <pre><code>&lt;UserControl
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      x:Class="wpfStackOverflow.BGrid"
      DataContext="{Binding RelativeSource={RelativeSource Self}, Path=B}"
      &gt;
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="Auto"/&gt;
              &lt;ColumnDefinition /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
  
          &lt;Label Grid.Column="0" Content="Property"/&gt;
          &lt;TextBox Grid.Column="1" Text="{Binding Property}"/&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>BGrid.xaml.cs</strong></p>
  
  <pre><code>public partial class BGrid : UserControl {
      static public DependencyProperty BProperty { get; private set; }
  
      static BGrid() {
          BProperty = DependencyProperty.Register("B", typeof(B), typeof(BGrid));
      }
  
      public B B {
          get { return (B)GetValue(BProperty); }
          set { SetValue(BProperty, value); }
      }
  
      public BGrid() {
          InitializeComponent();
      }
  }
  </code></pre>
  
  <p><strong>CGrid.xaml</strong></p>
  
  <pre><code>&lt;UserControl 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      x:Class="wpfStackOverflow.CGrid"
      DataContext="{Binding RelativeSource={RelativeSource Self}, Path=C}"
      &gt;
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="Auto"/&gt;
              &lt;ColumnDefinition /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
  
          &lt;Label Grid.Column="0" Content="Property"/&gt;
          &lt;TextBox Grid.Column="1" Text="{Binding Property}"/&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>CGrid.xaml.cs</strong></p>
  
  <pre><code>public partial class CGrid : UserControl {
      static public DependencyProperty CProperty { get; private set; }
  
      static CGrid() {
          CProperty = DependencyProperty.Register("C", typeof(C), typeof(CGrid));
      }
  
      public C C {
          get { return (C)GetValue(CProperty); }
          set { SetValue(CProperty, value); }
      }
  
      public CGrid() {
          InitializeComponent();
      }
  }
  </code></pre>
  
  <p><strong>window1.xaml</strong></p>
  
  <pre><code>&lt;Window 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:wpfStackOverflow"
      x:Class="wpfStackOverflow.Window1"
      Width="400"
      Height="200"
  &gt;
      &lt;local:AGroupBox x:Name="aGroupBox" /&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>Window1.xaml.cs</strong></p>
  
  <pre><code>public partial class Window1 : Window {
      public Window1() {
          InitializeComponent();
  
          aGroupBox.A = new A()
          {
              Property = "A's Custom Property Value",
              B = new B()
              {
                  Property = "B's Custom Property Value"
              },
              C = new C()
              {
                  Property = "C's Custom Property Value"
              }
          };
      }
  }
  </code></pre>