title: wpf mvvm nested user controls datacontext
body: <p>i have a problem with the <code>datacontext</code> of nested <code>user-controls</code>. The Problem is that the Model in the ViewModel is binded on a <code>DependencyProperty</code> of the first <code>user-control</code>. In the first <code>user-control</code> is a second <code>user-control</code>. The second <code>user-control</code> has also a <code>DependencyProperty</code>. The first <code>user-control</code> binds on that <code>DependencyProperty</code> of the second <code>user-control</code>.</p>
  
  <p><strong>Mainview:</strong></p>
  
  <pre><code>&lt;UserControl x:Class="XXX.XXX.Hmi.Views.L1SetupViewerView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               xmlns:prism="http://prismlibrary.com/"
               xmlns:uc="clr-namespace:XXX.XXX.Hmi.UserControls"
               d:DesignHeight="450"
               d:DesignWidth="800"
               prism:ViewModelLocator.AutoWireViewModel="True"
               Background="{DynamicResource BaseBrush}"
               Foreground="{DynamicResource TextBrush}"
               mc:Ignorable="d"&gt;
  .
  .
  .
      &lt;GroupBox Grid.Row="1" Header="Entry Section"&gt;
          &lt;uc:L1SetupViewerEntrySectionView Margin="8" Setup="{Binding L1Setup}" /&gt;
      &lt;/GroupBox&gt;
  .
  .
  .
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>MainViewModel:</strong></p>
  
  <pre><code>public class L1SetupViewerViewModel : ViewModelBase
  {
      private L1Setup _l1Setup;
      private Pdi _pdi;
  
      public L1SetupViewerViewModel()
      {
          Title = LanguageProvider.Instance.GetValue("L1SetupViewer_Window_Title");
          LanguageProvider.Instance.UiCultureChanged += Instance_UiCultureChanged;
      }
  
      #region Properties
  
      public L1Setup L1Setup
      {
          get { return _l1Setup; }
          set { SetProperty(ref _l1Setup, value); }
      }
      public Pdi Pdi
      {
          get { return _pdi; }
          set { SetProperty(ref _pdi, value); }
      }
  
      #endregion
  
      #region Navigation
  
      public override void OnNavigatedTo(NavigationContext navigationContext)
      {
          if (navigationContext.Parameters.ContainsKey("L1Setup"))
          {
              L1Setup = navigationContext.Parameters.GetValue&lt;L1Setup&gt;("L1Setup");
          }
  
          if (navigationContext.Parameters.ContainsKey("Pdi"))
          {
              Pdi = navigationContext.Parameters.GetValue&lt;Pdi&gt;("Pdi");
          }
      }
  
      #endregion
  
      private void Instance_UiCultureChanged(object sender, EventArgs e)
      {
          Title = LanguageProvider.Instance.GetValue("L1SetupViewer_Window_Title");
      }
  }
  </code></pre>
  
  <p><strong>First UserControl:</strong></p>
  
  <pre><code>&lt;UserControl x:Class="XXX.XXX.Hmi.UserControls.L1SetupViewerEntrySectionView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               xmlns:uc="clr-namespace:XXX.XXX.Hmi.UserControls"
               d:DesignHeight="450"
               d:DesignWidth="800"
               mc:Ignorable="d"&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="auto" /&gt;
              &lt;RowDefinition Height="auto" /&gt;
              &lt;RowDefinition Height="auto" /&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="*" SharedSizeGroup="c" /&gt;
              &lt;ColumnDefinition Width="*" SharedSizeGroup="c" /&gt;
              &lt;ColumnDefinition Width="*" SharedSizeGroup="c" /&gt;
              &lt;ColumnDefinition Width="*" SharedSizeGroup="c" /&gt;
              &lt;ColumnDefinition Width="*" SharedSizeGroup="c" /&gt;
              &lt;ColumnDefinition Width="*" SharedSizeGroup="c" /&gt;
              &lt;ColumnDefinition Width="*" SharedSizeGroup="c" /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;uc:L1SetupViewerCellControl Title="Over Speed"
                                       Grid.Row="0"
                                       Grid.Column="0"
                                       Margin="0,0,5,0"
                                       Value="{Binding Setup.EnOveSpd}" /&gt;
           .
           .
           .
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>First UserControl CodeBehind:</strong></p>
  
  <pre><code>public partial class L1SetupViewerEntrySectionView : UserControl
  {
      public L1Setup Setup
      {
          get { return (L1Setup)GetValue(SetupProperty); }
          set { SetValue(SetupProperty, value); }
      }
      public static readonly DependencyProperty SetupProperty =
          DependencyProperty.Register("Setup", typeof(L1Setup), typeof(L1SetupViewerEntrySectionView), new PropertyMetadata(null));
  
      public L1SetupViewerEntrySectionView()
      {
          InitializeComponent();
          this.DataContext = this;
      }
  }
  </code></pre>
  
  <p><strong>Second UserControl:</strong></p>
  
  <pre><code>&lt;UserControl x:Class="XXX.XXX.Hmi.UserControls.L1SetupViewerCellControl"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               d:DesignHeight="450"
               d:DesignWidth="800"
               mc:Ignorable="d"&gt;
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="auto" /&gt;
              &lt;ColumnDefinition Width="*" /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;TextBlock Grid.Column="0"
                     Margin="0,0,3,0"
                     Text="{Binding Title}" /&gt;
          &lt;TextBox Grid.Column="1" Text="{Binding Value}" /&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>Second UserControl CodeBehind:</strong></p>
  
  <pre><code>public partial class L1SetupViewerCellControl : UserControl
  {
      public string Title
      {
          get { return (string)GetValue(TitleProperty); }
          set { SetValue(TitleProperty, value); }
      }
      public static readonly DependencyProperty TitleProperty =
          DependencyProperty.Register("Title", typeof(string), typeof(L1SetupViewerCellControl), new PropertyMetadata(string.Empty));
  
      public string Value
      {
          get { return (string)GetValue(ValueProperty); }
          set { SetValue(ValueProperty, value); }
      }
      public static readonly DependencyProperty ValueProperty =
          DependencyProperty.Register("Value", typeof(string), typeof(L1SetupViewerCellControl), new PropertyMetadata(string.Empty));
  
      public L1SetupViewerCellControl()
      {
          InitializeComponent();
          this.DataContext = this;
      }
  }
  </code></pre>
  
  <p>If i run the application i get the following binding error:</p>
  
  <pre><code>System.Windows.Data Error: 40 : BindingExpression path error: 'L1Setup' property not found on 'object' ''L1SetupViewerCellControl' (Name='')'. BindingExpression:Path=L1Setup.EnPor1OutDia; DataItem='L1SetupViewerCellControl' (Name=''); target element is 'L1SetupViewerCellControl' (Name=''); target property is 'Value' (type 'String')
  
  </code></pre>
  
  <p>I tried several answers on stackOverflow, nothing worked for me and i don't get it whats wrong.</p>