title: WPF + MVVM - How to bind a property to the parent view's data context
body: <p>Working with the MVVM pattern, I have a pair of view model classes that represent a two-tier data hierarchy, each with a corresponding <code>UserControl</code> that represents its view. Both view model classes implement <code>INotifyPropertyChanged</code> and the root level view model exposes a property that is relevant to both its own view and the child view.</p>
  
  <p>The root level view acquires the root level view model as its data context and explicitly assigns a data context to its contained view. However, it also needs to bind one of the properties of the child view to the above-mentioned shared property. Here is how I have attempted to achieve this, but it's not working:</p>
  
  <pre><code>&lt;UserControl x:Name="rootView"&gt;
      &lt;StackPanel&gt;
  
          &lt;!-- other controls here --&gt;
  
          &lt;my:ChildView
              DataContext="{Binding Path=SelectedChild}"
              EditingMode="{Binding ElementName=rootView, Path=DataContext.EditingMode /&gt;
      &lt;/StackPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Although there are no runtime binding errors and the child view correctly binds to the appropriate child view model instance, its <code>EditingMode</code> property is never set. I have run tests to verify that the corresponding view model property is being modified and that it is notifying this change via <code>INotifyPropertyChanged</code>, but the binding fails to detect it.</p>
  
  <p>Is there a better way to declare this binding or have I made a more basic architectural error?</p>
  
  <p>Many thanks for your advice,</p>
  
  <p>Tim</p>
  
  <p><b>Update:</b> As requested, I am posting some code to show a very simplified version of my views and view models, together with the results of an experiment that I have conducted that may provide some additional clues.</p>
  
  <pre><code>// The relevant parts of the ParentViewModel class
  public class ParentViewModel : INotifyPropertyChanged
  {
      // Although not shown, the following properties
      // correctly participate in INotifyPropertyChanged
  
      public ChildViewModel SelectedChild { get; private set; }
  
      public ContentEditingMode EditingMode { get; private set; }
  }
  
  // The relevant parts of the ChildViewModel class
  public class ChildViewModel : INotifyPropertyChanged
  {
      // No properties of ChildViewModel affect this issue.
  }
  
  // The relevant parts of the ParentView class
  public partial class ParentView : UserControl
  {
      // No properties of ParentView affect this issue.
  }
  
  // The relevant members of the ChildView class
  public partial class ChildView : UserControl
  {
      public static readonly DependencyProperty EditingModeProperty =
          DependencyProperty.Register(
              "EditingMode",
              typeof(ContentEditingMode),
              typeof(PostView)
          );
  
      public ContentEditingMode EditingMode
      {
          get { return (ContentEditingMode)GetValue(EditingModeProperty); }
          set { SetValue(EditingModeProperty, value); }
      }
  }
  
  // The enumeration used for the EditingMode property
  public enum ContentEditingMode
  {
      Html,
      WYSYWIG
  }
  </code></pre>
  
  <p>My intention is that the <code>DataContext</code> of the parent view instance will be assigned an instance of <code>ParentViewModel</code> and it will, in turn, assign the value of its <code>SelectedChild</code> property to the <code>DataContext</code> of the nested <code>ChildView</code>. All of this seems to work correctly, but the problem arises because the binding between <code>ParentViewModel.EditingMode</code> and <code>ChildView.EditingMode</code> does not work.</p>
  
  <p>In an attempt to test whether there is a problem with my binding expression, I introduced a <code>TextBlock</code> adjacent to the <code>ChildView</code> and bound it similarly to the <code>ParentViewModel.EditingMode</code> property:  </p>
  
  <pre><code>&lt;UserControl x:Name="rootView"&gt;
      &lt;StackPanel&gt;
  
          &lt;!-- other controls here --&gt;
  
          &lt;TextBlock Text="{Binding ElementName=rootView, Path=DataContext.EditingMode}" /&gt;
  
          &lt;my:ChildView
              DataContext="{Binding Path=SelectedChild}"
              EditingMode="{Binding ElementName=rootView, Path=DataContext.EditingMode /&gt;
      &lt;/StackPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>In this test, the <code>TextBlock</code> is correctly updated every time the source property changes. However, if I set a breakpoint on the setter of <code>ChildView.EditingMode</code>, it never gets hit.</p>
  
  <p>I'm baffled !</p>