title: How to select a databound TreeViewItem?
body: <p>I have a XML databound to a TreeView with a XmlDataProvider. If i add a subnode to the XML the TreeView shows it, but how can i select this item?</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;HierarchicalDataTemplate DataType="category" ItemsSource="{Binding XPath=child::node()}"&gt;
          &lt;TextBlock Text="{Binding XPath=@name}" FontWeight="Bold" /&gt;
      &lt;/HierarchicalDataTemplate&gt;
  
      &lt;HierarchicalDataTemplate DataType="card"&gt;
          &lt;TextBlock Text="{Binding XPath=./title}" FontStyle="Italic" /&gt;
      &lt;/HierarchicalDataTemplate&gt;
  
      &lt;XmlDataProvider x:Key="dataxml" XPath="root/cards"/&gt;
  
  &lt;/Window.Resources&gt;
  
   &lt;TreeView  Name="treeView" 
              ItemsSource="{Binding Source={StaticResource dataxml}, 
                            XPath=./*, 
                            UpdateSourceTrigger=PropertyChanged}"
   /&gt;
  </code></pre>
  
  <p>CS:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      XmlDataProvider xmlDataProvider = new XmlDataProvider();
  
      public MainWindow()
      {
          InitializeComponent();           
          xmlDataProvider = this.FindResource("dataxml") as XmlDataProvider;
          xmlDataProvider.Source = new Uri(System.IO.Path.GetFullPath(fullPathToXml), UriKind.Absolute);
          xmlDataProvider.Refresh();
      }
  
      public void AddChild()
      {
          XmlNode newNode = xmlDataProvider.Document.CreateElement("card");
          XmlNode selectedItem = (XmlNode)treeView.SelectedItem;
  
          if (selectedItem != null)
          {
              //add the newNode as child to the selected
              selectedItem.AppendChild(newNode);
              //select the childnode (newNode) ?????       &lt;=====
          }
          else
          {
              //add the newNode as child to the rootnode and select it:
              xmlDataProvider.Document.DocumentElement["cards"].AppendChild(newNode);
              (treeView.ItemContainerGenerator.ContainerFromItem(newNode) as TreeViewItem).IsSelected = true;
          }
  
          xmlDataProvider.Document.Save(fullPathToXml);
          xmlDataProvider.Refresh();
      }
  }
  </code></pre>
  
  <p>XML:</p>
  
  <pre><code>&lt;root&gt;
    &lt;settings&gt;
      ....
      ..
    &lt;/settings&gt;
    &lt;cards&gt;
      &lt;category name="C1"&gt;
          &lt;card name="card1"&gt;
              &lt;question&gt;bla&lt;/question&gt;
              &lt;answer&gt;blub&lt;/answer&gt;
          &lt;/card&gt;
          &lt;category name="C2"&gt;
              &lt;card name="card4"&gt;
                  &lt;question&gt;bla&lt;/question&gt;
                  &lt;answer&gt;blub&lt;/answer&gt;
              &lt;/card&gt;
          &lt;/category&gt;
      &lt;/category&gt;
          &lt;card name="card2"&gt;
              &lt;question&gt;bla&lt;/question&gt;
              &lt;answer&gt;blub&lt;/answer&gt;
          &lt;/card&gt;
          &lt;card name="card3"&gt;
              &lt;question&gt;bla&lt;/question&gt;
              &lt;answer&gt;blub&lt;/answer&gt;
          &lt;/card&gt;
    &lt;/cards&gt;
  &lt;/root&gt;
  </code></pre>