title: SelectedItem in TreeView
body: <p>I am trying to get the selected item from my TreeView but having some problem.</p>
  
  <p>I am following MVVM archetecture. My ViewModel contains a collection of a class which is in my Model. So I have binded the ItemSource of TreeView with that collection. I want to bind the selectedItem of my TreeView to an item of the binded collection. How do I do that? Here is the code for SelectedItem and IsSelected Property.</p>
  
  <pre><code>    private static sourceData _selectedItem = null;
      /// &lt;summary&gt;
      /// Selected Item in the tree
      /// &lt;/summary&gt;
      public static sourceData SelectedItem
      {
          get { return _selectedItem; }
          set
          {
              if (_selectedItem != value)
              {
                  _selectedItem = value;
              }
          }
      }
  
      private bool _isSelected;
      /// &lt;summary&gt;
      /// Get/Set for Selected node
      /// &lt;/summary&gt;
      public bool IsSelected
      {
          get { return _isSelected; }
          set
          {
              if (_isSelected != value)
              {
                  _isSelected = value;
  
                  if (_isSelected)
                  {
                      SelectedItem = this;
                      OnPropertyChanged("IsSelected");
                  }
              }
          }
      }
  
      /// &lt;summary&gt;
      /// Property changed event
      /// &lt;/summary&gt;
      public event PropertyChangedEventHandler PropertyChanged;
      /// &lt;summary&gt;
      /// Property changed event handler
      /// &lt;/summary&gt;
      /// &lt;param name="propertyName"&gt;&lt;/param&gt;
      protected virtual void OnPropertyChanged(string propertyName)
      {
          var handler = this.PropertyChanged;
          if (handler != null)
              handler(this, new PropertyChangedEventArgs(propertyName));
      }
  </code></pre>
  
  <p>When I debug this, int <code>SelectedItem = this;</code> 'this' pointer contains the collection to which my treeview is binded. I needed to have a SelectedDataSource so that I could assign it to the selected Item. How can I make my TreeView return me the selectedItem in the collection??</p>
  
  <p>FYi, this is my XAML code for TreeView</p>
  
  <pre><code>&lt;TreeView Margin="5,0,0,0" ItemsSource="{Binding SourceData}"  Width="390"&gt;
                      &lt;TreeView.ItemContainerStyle&gt;
                          &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
                              &lt;Setter Property="IsSelected" Value="{Binding DataContext.IsSelected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" /&gt;
                            &lt;Setter Property="ContextMenu"&gt;
                                  &lt;Setter.Value&gt;
                                      &lt;ContextMenu Name="contextMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" &gt;
                                          &lt;MenuItem Name="menuItem" Header="Rename" Command="{Binding RenameCommand}" /&gt;
                                      &lt;/ContextMenu&gt;
                                  &lt;/Setter.Value&gt;
                              &lt;/Setter&gt;
                              &lt;Style.Triggers&gt;
                                  &lt;Trigger Property="IsSelected" Value="True"&gt;
                                  &lt;/Trigger&gt;
                              &lt;/Style.Triggers&gt;
                          &lt;/Style&gt;
                      &lt;/TreeView.ItemContainerStyle&gt;
  </code></pre>
  
  <p>PS: If I write the above code in my Model, I get everything working perfectly fine. But I can not write the above code in Model, it has to be in VM. </p>