title: TreeView doesn't refresh after changes
body: <p>I write a program for edition of IIS web.config file. I have a problem with my TreeView control which doesn't want to refresh itself after I change something in the source XmlDocument variable.<br />
  It's the WPF project.</p>
  
  <p>Window resources in XAML:</p>
  
  <pre><code>&lt;Window.Resources&gt;
     &lt;XmlDataProvider x:Key="XmlData" /&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>My TreeView:</p>
  
  <pre><code>&lt;TreeView x:Name="XmlTree" Grid.Row="1"
     ItemsSource="{Binding Source={StaticResource XmlData}, XPath=*}"
     ItemTemplate="{StaticResource NodeTemplate}"
     SelectedItemChanged="XmlTree_SelectedItemChanged" /&gt;
  </code></pre>
  
  <p>TreeView style:</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}"
               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}"/&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;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>private XmlDocument _xml;
  private XmlElement _selectedElement; // actually selected item in TreeView
  private XmlDataProvider _xmlDataProvider;
  private string _tempFileName = @"C:\test.xml";
  
  public MainWindow()
  {
      InitializeComponent();
  
      XmlTree.Style = (Style)FindResource("TreeViewAllExpandedStyle");
      _xmlDataProvider = FindResource("XmlData") as XmlDataProvider;
  }
  
  private void OpenXmlFile(string filePath)
  {
      XmlEditor.Clear(); // my text editor provided by AvalonEdit
      XmlEditor.Load(filePath);
  
      _xml = new XmlDocument();
      _xml.Load(filePath);
  
      if(_xmlDataProvider.Document == null)
          _xmlDataProvider.Document = _xml;
  }
  
  private void saveChangesButton_Click(object sender, EventArgs e)
  {
      // some changes on _selectedElement (changes applies also into _xml)
  
      _xml.Save(_tempFileName);
      RefreshViews();
  }
  
  private void RefreshViews()
  {
      // 
      OpenXmlFile(_tempFileName);
  
      // here I want to refresh my TreeView
      // I noticed that when I select back my changed item, its values are set, but in my tree I see the old ones...
      // I tried to do XmlTree.Focus() (nothing happens excepting control focus)
      // and _xmlDataProvider.Refresh() (here comes NullReferenceException)
      // I guess something bad happens in OpenXmlFile(...) method, because I reopen _xml and _xmlDataProvider looses a handler to it?
  }
  </code></pre>
  
  <p>Could anyone explain why it doesn't work?</p>
  
  <p><b>[edit]</b></p>
  
  <p>I tried to modify 2 methods like this:</p>
  
  <pre><code>private void OpenXmlFile(string filePath)
  {
      XmlEditor.Clear();
      XmlEditor.Load(filePath);
  
      _xml = new XmlDocument();
      _xml.Load(filePath);
  
      _xmlDataProvider.Document = _xml;
  }
  
  private void saveChangesButton_Click(object sender, EventArgs e)
  {
      // ...
  
      _xmlDataProvider.Document.Save(_tempFileName);
      _xmlDataProvider.Refresh();
  }
  </code></pre>
  
  <p>And now I get NullReferenceException while <code>_xmlDataProvider.Refresh();</code></p>