更改后TreeView不会刷新

时间:2012-08-06 13:19:18

标签: c# wpf xml treeview

我为IIS web.config文件编写了一个程序。我的TreeView控件有问题,在我更改源XmlDocument变量中的内容后不想刷新自己。
这是WPF项目。

XAML中的窗口资源:

<Window.Resources>
   <XmlDataProvider x:Key="XmlData" />
</Window.Resources>

我的TreeView:

<TreeView x:Name="XmlTree" Grid.Row="1"
   ItemsSource="{Binding Source={StaticResource XmlData}, XPath=*}"
   ItemTemplate="{StaticResource NodeTemplate}"
   SelectedItemChanged="XmlTree_SelectedItemChanged" />

TreeView样式:

<DataTemplate x:Key="AttributeTemplate">
    <StackPanel Orientation="Horizontal"
            Margin="3,0,0,0"
            HorizontalAlignment="Center">
        <TextBlock Text="{Binding Path=Name}"
             Foreground="{StaticResource xmAttributeBrush}" FontFamily="Consolas" FontSize="8pt" />
        <TextBlock Text="=&quot;"
             Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
        <TextBlock Text="{Binding Path=Value}"
             Foreground="{StaticResource xmlValueBrush}" FontFamily="Consolas" FontSize="8pt" />
        <TextBlock Text="&quot;"
             Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
    </StackPanel>
</DataTemplate>

<HierarchicalDataTemplate x:Key="NodeTemplate">
    <StackPanel Orientation="Horizontal" Focusable="False">
        <TextBlock x:Name="tbName" Text="?" FontFamily="Consolas" FontSize="8pt" />
        <ItemsControl
            ItemTemplate="{StaticResource AttributeTemplate}"
            ItemsSource="{Binding Path=Attributes}"
            HorizontalAlignment="Center">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </StackPanel>
    <HierarchicalDataTemplate.ItemsSource>
        <Binding XPath="*" />
    </HierarchicalDataTemplate.ItemsSource>
    <HierarchicalDataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
            <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Value}"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
            <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}"/>
        </DataTrigger>
    </HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>

代码背后:

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?
}

有人可以解释为什么它不起作用吗?

[编辑]

我尝试修改这样的两种方法:

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();
}

现在我在_xmlDataProvider.Refresh();

时得到NullReferenceException

2 个答案:

答案 0 :(得分:2)

您好,您可以尝试在TreeView和可观察集合上使用Binding Two-Way。

{Binding .... Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}

Firslty你可以修改你的绑定

ItemsSource="{Binding Source={StaticResource XmlData}, XPath=*, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

答案 1 :(得分:1)

如果xmlDataProvider.Document函数为空,则只在OpenXmlFile()函数中设置_xml = new XmlDocument();的值。当你设置 _xml,它将xmlDataProvider设置为指向新对象,但if(_xmlDataProvider.Document == null) _xmlDataProvider.Document = _xml; 仍指向旧对象。然后,您有以下两行:

RefreshViews()

如果您从_xmlDataProvider.Document进入此处,{{1}}将不会为空,那么您永远不会刷新与您的数据提供商相关联的XML文件。