title: Get selected TreeViewItem in MVVM
body: <p>I want make TreeView with editable nodes. I googled this good, as I think, article:</p>
  
  <p><a href="http://www.codeproject.com/Articles/31592/Editable-TextBlock-in-WPF-for-In-place-Editing" rel="nofollow">http://www.codeproject.com/Articles/31592/Editable-TextBlock-in-WPF-for-In-place-Editing</a></p>
  
  <p>But I have a problems. My TreeView formed dinamically, not statically as in the arcticle. Like that</p>
  
  <pre><code>    &lt;TreeView Name="_packageTreeView" Margin="5" ItemsSource="{Binding PackageExtendedList}"&gt;
           &lt;TreeView.InputBindings&gt;
               &lt;KeyBinding Key="C" Command="{Binding *TestCommand*}" CommandParameter="{Binding}" /&gt;
           &lt;/TreeView.InputBindings&gt;
           &lt;i:Interaction.Triggers&gt;
               &lt;i:EventTrigger EventName="SelectedItemChanged"&gt;
                   &lt;i:InvokeCommandAction Command="{Binding PackageTreeItemChangeCommand}" CommandParameter="{Binding ElementName=_packageTreeView, Path=SelectedItem}"/&gt;
               &lt;/i:EventTrigger&gt;
           &lt;/i:Interaction.Triggers&gt;
           &lt;TreeView.ItemContainerStyle&gt;
               &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
                   &lt;Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" /&gt;
               &lt;/Style&gt;
           &lt;/TreeView.ItemContainerStyle&gt;
           &lt;TreeView.ItemTemplate&gt;
               &lt;HierarchicalDataTemplate DataType="{x:Type MasterBuisnessLogic:RootDocPackage}" ItemsSource="{Binding Path=Childs}"&gt;
                   &lt;Grid&gt;
                       &lt;Grid.ColumnDefinitions&gt;
                           &lt;ColumnDefinition&gt;
                           &lt;/ColumnDefinition&gt;
                           &lt;ColumnDefinition&gt;
                           &lt;/ColumnDefinition&gt;
                       &lt;/Grid.ColumnDefinitions&gt;
                      &lt;Image Source="/Resources/DocGroup.png"&gt;&lt;/Image&gt;
                      &lt;Etb:EditableTextBlock Margin="5,0,0,0" Grid.Column="1" Text="{Binding Path=Name}"&gt;&lt;/Etb:EditableTextBlock&gt;
                   &lt;/Grid&gt; 
               &lt;/HierarchicalDataTemplate&gt;
           &lt;/TreeView.ItemTemplate&gt;
       &lt;/TreeView&gt;
  </code></pre>
  
  <p><em>PackageExtendedList</em> - List of DocPackageExtended.</p>
  
  <p>So, first question - how can I get TreeViewItem instance in TestCommand? Not instance <em>DocPackageExtended</em> class! I want to get instance selected <em>TreeViewItem</em> like in the article.</p>
  
  <p>And second question - After I get instance <em>TreeViewItem</em>, how can I get <em>EditableTextBlock</em> from the TreeView item's DataTemplate.</p>
  
  <p><strong>added answer</strong> </p>
  
  <p>I already tried it. Cause in MVVM ViewModel cannot has any link to View object like TreeView, I make handler in code-behind, like that</p>
  
  <pre><code>private void TreeViewItemSelected(object sender, RoutedEventArgs e)
  {
      // Already have TreeViewItem instance without of ItemContainerGenerator help
      var tvi = e.OriginalSource as TreeViewItem;
  
      if (tvi == null)
          return;
  
      var etb = VisualTreeLib.VisualTreeLib.GetVisualChild&lt;EditableTextBlock&gt;(tvi);
      if (etb == null)
          return;
  
      // Do what I want
      etb.IsEditable = true;
  }
  </code></pre>
  
  <p>Unfortunately, this has no any affect :(</p>
  
  <p>I also tried that approach, but also failed.</p>
  
  <p>in DocPackageExtended type I define property</p>
  
  <pre><code>public bool IsEditable
  {
      get { return _isEditable; }
      set
      {
          _isEditable = value;
          OnPropertyChanged(new PropertyChangedEventArgs("IsEditable"));
      }
  }
  </code></pre>
  
  <p>than change in XAML:</p>
  
  <pre><code>&lt;Etb:EditableTextBlock Margin="5,0,0,0" Grid.Column="1" Text="{Binding Path=Name}" *IsEditable="{Binding Path=IsEditable}"*/&gt;
  </code></pre>
  
  <p>and in ViewModel</p>
  
  <pre><code>private void TestCommandMethod(object obj)
  {
      var dpe = obj as DocPackageExtended;
      if (dpe == null)
         return;
      dpe.IsEditable = true;
  }
  </code></pre>
  
  <p>Doesn't work too :(
  Any ideas?</p>