title: WPF Databinding searching in wrong DataContext
body: <p>i'm learning the MVVM Pattern and have some trouble with Databindings. I understand how it works, but in my example it's using the wrong DataContext to search for the binding.</p>
  
  <p>I did not found another Question that suits to my problem.</p>
  
  <p>So i have this View:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;UserControl x:Class="Kenshinaro.CashRegister.UI.View.CashRegister.CashRegisterChoiceView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               mc:Ignorable="d" 
               d:DesignHeight="450" d:DesignWidth="800"&gt;
      &lt;ItemsControl ItemsSource="{Binding CashRegisters}"&gt;
          &lt;ItemsControl.ItemsPanel&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;WrapPanel&gt;&lt;/WrapPanel&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/ItemsControl.ItemsPanel&gt;
      &lt;/ItemsControl&gt;
  &lt;/UserControl&gt;
  
  </code></pre>
  
  <p>With following ViewModel:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Kenshinaro.CashRegister.UI.View.CashRegister;
  
  namespace Kenshinaro.CashRegister.UI.ViewModel.CashRegister
  {
      public class CashRegisterChoiceViewModel : MVVM.ViewModel
      {
          private ObservableCollection&lt;CashRegisterItemsControlView&gt; _cashRegisters = new ObservableCollection&lt;CashRegisterItemsControlView&gt;()
          {
              new CashRegisterItemsControlView()
              {
                  DataContext = new CashRegisterItemsControlViewModel()
                  {
                      View = new CashRegisterCardView()
                      {
                          DataContext = new CashRegisterCardViewModel()
                          {
                              Model = new Model.CashRegister()
                              {
                                  Name = "1"
                              }
                          }
                      }
                  }
              },
              new CashRegisterItemsControlView()
              {
                  DataContext = new CashRegisterItemsControlViewModel()
                  {
                      View = new CashRegisterCardView()
                      {
                          DataContext = new CashRegisterCardViewModel()
                          {
                              Model = new Model.CashRegister()
                              {
                                  Name = "2"
                              }
                          }
                      }
                  }
              },
              new CashRegisterItemsControlView()
              {
                  DataContext = new CashRegisterItemsControlViewModel()
                  {
                      View = new CashRegisterCardView()
                      {
                          DataContext = new CashRegisterCardViewModel()
                          {
                              Model = new Model.CashRegister()
                              {
                                  Name = "3"
                              }
                          }
                      }
                  }
              }
          };
  
          public ObservableCollection&lt;CashRegisterItemsControlView&gt; CashRegisters
          {
              get =&gt; _cashRegisters;
              set =&gt; SetProperty(ref _cashRegisters, value);
          }
      }
  }
  </code></pre>
  
  <p>(My Base ViewModel class is implementing the INotifyPropertyChanged interface)</p>
  
  <p>So this View shows a list of this view:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;UserControl x:Class="Kenshinaro.CashRegister.UI.View.CashRegister.CashRegisterItemsControlView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:local="clr-namespace:Kenshinaro.CashRegister.UI.View.CashRegister"
               xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
               mc:Ignorable="d" 
               d:DesignHeight="450" d:DesignWidth="800"&gt;
      &lt;md:Card Margin="10"&gt;
          &lt;Grid &gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="Auto"/&gt;
                  &lt;RowDefinition Height="Auto"/&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;local:CashRegisterCardView Padding="5"/&gt;
              &lt;Button Margin="20" Grid.Row="1" Content="Pick me"/&gt;
          &lt;/Grid&gt;
      &lt;/md:Card&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>ViewModel:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace Kenshinaro.CashRegister.UI.ViewModel.CashRegister
  {
      public class CashRegisterItemsControlViewModel : MVVM.ViewModel
      {
          private View.CashRegister.CashRegisterCardView _view;
  
          public View.CashRegister.CashRegisterCardView View
          {
              get =&gt; _view;
              set =&gt; SetProperty(ref _view, value);
          }
      }
  }
  </code></pre>
  
  <p>And this View shows also this view:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;UserControl x:Class="Kenshinaro.CashRegister.UI.View.CashRegister.CashRegisterCardView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:local="clr-namespace:Kenshinaro.CashRegister.UI.View.CashRegister"
               xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
               xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
               mc:Ignorable="d" 
               d:DesignHeight="450" d:DesignWidth="800" Background="White" &gt;
      &lt;Grid&gt;
          &lt;TextBlock Text="{Binding DataContext.Name, RelativeSource={RelativeSource AncestorType=local:CashRegisterCardView}}"/&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>ViewModel:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Kenshinaro.CashRegister.Model;
  
  namespace Kenshinaro.CashRegister.UI.ViewModel.CashRegister
  {
      public class CashRegisterCardViewModel : MVVM.ViewModel
      {
          private Model.CashRegister _model;
  
          public Model.CashRegister Model
          {
              get =&gt; _model;
              set =&gt; SetProperty(ref _model, value);
          }
  
          public String Name
          {
              get =&gt; _model.Name;
              set =&gt; _model.Name = value;
          }
      }
  }
  </code></pre>
  
  <p>Now if I start the App the Binding on the TextBlock in the last view doesn't appear.
  The Output of VisualStudio says:</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 40 : BindingExpression path error: 'Name' property not found on 'object' ''CashRegisterItemsControlViewModel' (HashCode=35528341)'. BindingExpression:Path=DataContext.Name; DataItem='CashRegisterCardView' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')</p>
  </blockquote>
  
  <p>As I can get out of it, it is searching for the Property in the DataContext of CashRegisterItemsControlView but I just don't understand why, because i set the DataContext manually on collection initialization (Just for testing):</p>
  
  <pre class="lang-cs prettyprint-override"><code>private ObservableCollection&lt;CashRegisterItemsControlView&gt; _cashRegisters = new ObservableCollection&lt;CashRegisterItemsControlView&gt;()
          {
              new CashRegisterItemsControlView()
              {
                  DataContext = new CashRegisterItemsControlViewModel()
                  {
                      View = new CashRegisterCardView()
                      {
                          DataContext = new CashRegisterCardViewModel()
                          {
                              Model = new Model.CashRegister()
                              {
                                  Name = "1"
                              }
                          }
                      }
                  }
              },
  ...
  </code></pre>
  
  <p>So why it's still taking the wrong DataContext?</p>
  
  <p>Now when i change the Binding to View.DataContext.Name it's searching in the manually set DataContext:</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 40 : BindingExpression path error: 'View' property not found on 'object' ''CashRegisterCardView' (Name='')'. BindingExpression:Path=View.DataContext.Name; DataItem='CashRegisterCardView' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')</p>
  </blockquote>
  
  <p>I'm really confused.. I hope you can help me.</p>
  
  <p>Thanks!</p>
  
  <p>If you need information about the other classes just ask. I don't want to put more code into the question as it just long enough.</p>