title: How to select WPF TreeViewItem which has been populated using XmlDataProvider?
body: <p>I have a WPF <code>TreeView</code> which has been populated from an XML file at runtime. I click on a button and a <code>FileDialog</code> box appears and then I select an XML file. Then the XML file is loaded in the <code>TreeView</code>. I have used a <code>XmlDataProvider</code> and some <code>HierarchicalDataTemplate</code>s to load the XML.</p>
  
  <p>I have added an action handler with the TreeView, but when I select an item of that TreeView I can't find any reference of that item. I only find the reference of the first element. </p>
  
  <p>How can I solve this issue?</p>
  
  <p>The XAML is:</p>
  
  <pre><code>&lt;Window x:Class="Demo2.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="600" Width="800"&gt;
  &lt;Window.Resources&gt;
      &lt;XmlDataProvider x:Key="MEIInformation" XPath="/MEI" /&gt;
      &lt;HierarchicalDataTemplate DataType="Case" ItemsSource="{Binding}"&gt;
          &lt;TextBlock Text="{Binding XPath=@Name}"&gt;&lt;/TextBlock&gt;            
      &lt;/HierarchicalDataTemplate&gt;
      &lt;HierarchicalDataTemplate DataType="Phase" ItemsSource="{Binding}"&gt;
          &lt;TextBlock Text="{Binding XPath=@Name}"&gt;&lt;/TextBlock&gt;
      &lt;/HierarchicalDataTemplate&gt;
      &lt;HierarchicalDataTemplate DataType="Trigger" ItemsSource="{Binding}"&gt;
          &lt;TextBlock Text="{Binding XPath=@Name}"&gt;&lt;/TextBlock&gt;
      &lt;/HierarchicalDataTemplate&gt;
  &lt;/Window.Resources&gt;  
  &lt;Grid&gt;
      &lt;ToolBarTray&gt;
          &lt;ToolBar&gt;                
              &lt;Button ToolTip="Open Test Suite" Click="OpenTestSuite"&gt;
                  &lt;Image Source="Icons/open.png"&gt;&lt;/Image&gt;
              &lt;/Button&gt;                
          &lt;/ToolBar&gt;
      &lt;/ToolBarTray&gt;
      &lt;TreeView x:Name="trv" FontSize="14" Height="518" HorizontalAlignment="Left" Margin="6,31,0,0"  VerticalAlignment="Top" Width="431"&gt;
          &lt;TreeViewItem ItemsSource="{Binding Source={StaticResource MEIInformation}, XPath=*}" Header="Suites"&gt;&lt;/TreeViewItem&gt;
      &lt;/TreeView&gt;                           
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And the code snippest is:</p>
  
  <pre><code>public partial class MainWindow : Window
  {     
      public MainWindow()
      {
          InitializeComponent();     
          this.trv.MouseRightButtonUp+=new MouseButtonEventHandler(DoSomething);      
      }    
  
      private void OpenTestSuite(object sender, RoutedEventArgs e)
      {
          XmlDocument xmlDocument = new XmlDocument();
          OpenFileDialog open = new OpenFileDialog();
          open.Filter = "XML Files (*.xml)|*.xml";
          if (open.ShowDialog() == true)
          {
              xmlDocument.Load(open.FileName);
              XmlDataProvider dataProvider = this.FindResource("MEIInformation") as XmlDataProvider;
              dataProvider.Document = xmlDocument;         
          }
      }        
  
      private void DoSomething(object sender, MouseEventArgs e)
      {
          MessageBox.Show("Do Something in TreeView!");
          TreeViewItem childItem = e.Source as TreeViewItem;
          if (childItem != null)
          {
              MessageBox.Show(childItem.Header.ToString()); // or MessageBox.Show(childItem.toString);
              childItem.IsSelected = true;
          }
          else
              MessageBox.Show("No Selected Item!");                    
      }                
  }
  </code></pre>
  
  <p>XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;MEI&gt;
      &lt;Case Name="Jean Price"&gt;
          &lt;Phase Name="January"&gt;
              &lt;Trigger Name="Order # JAN001"&gt;&lt;/Trigger&gt;
              &lt;Trigger Name="Order # JAN002"&gt;&lt;/Trigger&gt;
          &lt;/Phase&gt;
          &lt;Phase Name="February"&gt;
              &lt;Trigger Name="Order # FEB001"&gt;&lt;/Trigger&gt;
          &lt;/Phase&gt;
      &lt;/Case&gt;
      &lt;Case Name="John P Grant"&gt;
          &lt;Phase Name="April"&gt;
              &lt;Trigger Name="Order # APR001"&gt;&lt;/Trigger&gt;
      &lt;!-- ... --&gt;
  &lt;/MEI&gt;
  </code></pre>
  
  <p>I just want to have a reference of that item so that i can add new item after that item.</p>
  
  <hr>
  
  <p>@Mart
  I have changed the treeview on the xaml like this... (added SelectedItemChanged="DoSomething")...</p>
  
  <pre><code>&lt;TreeView SelectedItemChanged="DoSomething"....../&gt;    
  </code></pre>
  
  <p>And the c# code is now.......                                         </p>
  
  <pre><code>private void DoSomething(object sender, EventArgs e)
          {
              MessageBox.Show("Do Something in TreeView!");           
  
              TreeViewItem childItem = sender as TreeViewItem;                        
              if (childItem != null)
              {
                  MessageBox.Show(childItem.Header.ToString());
                  childItem.IsSelected = true;
                  MessageBox.Show("It Works!");
              }
              else
                  MessageBox.Show("No Selected Item!");                    
          }     
  </code></pre>
  
  <p>but there is no luck at all. And i have not understood ur UIElement sugestion...:(</p>