title: WPF TreeView refreshing
body: <p>I've got a problem. I use <code>TreeView</code> in my WPF project to visualize my XML data. The problem is, when I edit my <code>XmlDocument</code> it doesn't refresh in <code>TreeView</code>. But I noticed that when I check <code>SelectedNode</code>, it is my editted and <code>XmlNode</code>. So my "Edit" method works fine, but there's only a problem in visual refresh of my tree. <code>.Refresh()</code> or <code>.Items.Refresh()</code> don't work either.</p>
  
  <p>Here's the template of my tree:</p>
  
  <pre><code>&lt;DataTemplate x:Key="AttributeTemplate"&gt;
      &lt;StackPanel Orientation="Horizontal"
              Margin="3,0,0,0"
              HorizontalAlignment="Center"&gt;
          &lt;TextBlock Text="{Binding Path=Name}"
               Foreground="{StaticResource xmAttributeBrush}" FontFamily="Consolas" FontSize="8pt" /&gt;
          &lt;TextBlock Text="=&amp;quot;"
               Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" /&gt;
          &lt;TextBlock Text="{Binding Path=Value, Mode=TwoWay}"
               Foreground="{StaticResource xmlValueBrush}" FontFamily="Consolas" FontSize="8pt" /&gt;
          &lt;TextBlock Text="&amp;quot;"
               Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" /&gt;
      &lt;/StackPanel&gt;
  &lt;/DataTemplate&gt;
  
  &lt;HierarchicalDataTemplate x:Key="NodeTemplate"&gt;
      &lt;StackPanel Orientation="Horizontal" Focusable="False"&gt;
          &lt;TextBlock x:Name="tbName" Text="?" FontFamily="Consolas" FontSize="8pt" /&gt;
          &lt;ItemsControl
              ItemTemplate="{StaticResource AttributeTemplate}"
              ItemsSource="{Binding Path=Attributes}"
              HorizontalAlignment="Center"&gt;
              &lt;ItemsControl.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;StackPanel Orientation="Horizontal"/&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ItemsControl.ItemsPanel&gt;
          &lt;/ItemsControl&gt;
      &lt;/StackPanel&gt;
      &lt;HierarchicalDataTemplate.ItemsSource&gt;
          &lt;Binding XPath="*" /&gt;
      &lt;/HierarchicalDataTemplate.ItemsSource&gt;
      &lt;HierarchicalDataTemplate.Triggers&gt;
          &lt;DataTrigger Binding="{Binding Path=NodeType}" Value="Text"&gt;
              &lt;Setter TargetName="tbName" Property="Text" Value="{Binding Path=Value, Mode=TwoWay}"/&gt;
          &lt;/DataTrigger&gt;
          &lt;DataTrigger Binding="{Binding Path=NodeType}" Value="Element"&gt;
              &lt;Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}"/&gt;
          &lt;/DataTrigger&gt;
      &lt;/HierarchicalDataTemplate.Triggers&gt;
  &lt;/HierarchicalDataTemplate&gt;
  
  &lt;Style x:Key="TreeViewAllExpandedStyle"  TargetType="{x:Type TreeView}"&gt;
      &lt;Style.Resources&gt;
          &lt;Style TargetType="TreeViewItem"&gt;
              &lt;Setter Property="IsExpanded" Value="True" /&gt;
          &lt;/Style&gt;
      &lt;/Style.Resources&gt;
  &lt;/Style&gt;
  
  &lt;Style x:Key="TreeViewAllCollapsedStyle" TargetType="{x:Type TreeView}"&gt;
      &lt;Style.Resources&gt;
          &lt;Style TargetType="TreeViewItem"&gt;
              &lt;Setter Property="IsExpanded" Value="False" /&gt;
          &lt;/Style&gt;
      &lt;/Style.Resources&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Here are <code>Window.Resources</code>:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;XmlDataProvider x:Key="XmlData" /&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>Here's my tree:</p>
  
  <pre><code>&lt;TreeView x:Name="XmlTree" Grid.Row="1"
        ItemsSource="{Binding Source={StaticResource XmlData}, XPath=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
        ItemTemplate="{StaticResource NodeTemplate}"
        SelectedItemChanged="XmlTree_SelectedItemChanged" /&gt;
  </code></pre>
  
  <p>And here's my code behind:</p>
  
  <pre><code>private XmlDocument _xml;
  private XmlElement _selectedElement;
  private XmlDataProvider _xmlDataProvider;
  
  private void MainWindow_Load(object sender, EventArgs e)
  {
      XmlTree.Style = (Style)FindResource("TreeViewAllExpandedStyle");
      _xmlDataProvider = FindResource("XmlData") as XmlDataProvider;
  }
  
  private void OpenXmlFile(string filePath)
  {
      _xml = new XmlDocument();
      _xml.Load(filePath);
  
      _xmlDataProvider.Document = _xml;
  }
  
  private void SaveChangesButton_Click(object sender, EventArgs e)
  {
      Dictionary&lt;string, string&gt; newAttributes = GetChangedAttributes();
      foreach (KeyValuePair&lt;string, string&gt; pair in newAttributes)
      {
          _selectedElement.SetAttribute(pair.Key, pair.Value);
      }
  
      RefreshViews();
  }
  
  private void RefreshViews()
  {
      // now I don't know what to do here, any Refresh doesn't work:S
  }
  </code></pre>
  
  <p>The second thing is, how to clear my tree in order to be able to use it again for another data (I've got <code>NullReferenceException</code> while trying <code>XmlTree.Items.Clear();</code></p>