title: Bind SelectedItem to correct DataContext in WPF
body: <p>I have a UserControl <code>barView</code> and corresponding ViewModel <code>barViewModel</code>. This ViewModel have property <code>SelectedSomething</code>, which is binded to different ListBoxes in my view.</p>
  
  <p>If I have construction like this, then everything woks fine:</p>
  
  <pre><code>&lt;UserControl DataContext="barViewModel"&gt;
      &lt;ListBox ItemsSource="{Binding ObservableCollectionWithItems}"
               SelectedItem="{Binding SelectedSomething, Mode=TwoWay}"&gt;
          ....
      &lt;/ListBox&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>In this case my ViewModel have ObservableCollection with items.</p>
  
  <p>Now I want to split my items to groups. I create a separete class for that:</p>
  
  <pre><code>class ItemsGroup
  {
       private string _Name;
       public string Name {...}
  
       private List&lt;Item&gt; _ItemsList;
       public List&lt;Item&gt; ItemsList {...}
  }
  </code></pre>
  
  <p>My <code>barViewModel</code> now contains a observalbe collection of <code>ItemsGroup</code> objects. New view for this looks like this:</p>
  
  <pre><code>&lt;UserControl DataContext="barViewModel"&gt;
  &lt;ItemsControl ItemsSource="{Binding ObservalbeCollectionWithItemsGroup}"&gt;
      &lt;ItemsControl.ItemTemplate&gt;
          &lt;DataTemplate DataType="{x:Type test:ItemsGroup}"&gt;
              &lt;Expander IsExpanded="False"&gt;
                  &lt;Expander.Header&gt;
                      &lt;TextBlock Content="{Binding Name}"/&gt;
                  &lt;/Expander.Header&gt;
                  &lt;ListBox ItemsSource="{Binding ItemsList}" Margin="10"
                           SelectedItem="{Binding SelectedSomething, Mode=TwoWay}"&gt;
                      ...
                  &lt;/ListBox&gt;
              &lt;/Expander&gt;
          &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>
  
  <p></p>
  
  <p>The problem is that SelectedItem of ListBox is binded to the parent and show me this error:</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 40 : BindingExpression path error: 'SelectedSomething' property not found on 'object' ''ItemsGroup' (HashCode=10335672)'. BindingExpression:Path=SelectedSomething; DataItem='ItemsGroup' (HashCode=10335672); target element is 'ListBox' (Name=''); target property is 'SelectedItem' (type 'Object')</p>
  </blockquote>
  
  <p>I tried to change SelectedItem to this:</p>
  
  <pre><code>Text="{Binding SelectedSomething,
                 RelativeSource={RelativeSource Mode=FindAncestor,
                                                AncestorType={x:Type UserControl}}}"
  </code></pre>
  
  <p>This will remove error, but my <code>SelectedSomething</code> is still not binded to the ListBox. How can I fix this?</p>