将Xml数据双向绑定到WPF TreeView

时间:2008-10-09 16:13:29

标签: c# wpf xml data-binding treeview

我正在尝试使用WPF为表示层重写我的ForestPad应用程序。在WinForms中,我以编程方式填充每个节点,但如果可能的话,我想利用WPF的数据绑定功能。

一般来说,将WPF TreeView双向数据绑定到Xml文档的最佳方法是什么?

通用解决方案很好,但作为参考,我尝试绑定的Xml文档的结构如下所示:

<?xml version="1.0" encoding="utf-8"?>
<forestPad
    guid="6c9325de-dfbe-4878-9d91-1a9f1a7696b0"
    created="5/14/2004 1:05:10 AM"
    updated="5/14/2004 1:07:41 AM">
<forest 
    name="A forest node"
    guid="b441a196-7468-47c8-a010-7ff83429a37b"
    created="01/01/2003 1:00:00 AM"
    updated="5/14/2004 1:06:15 AM">
    <data>
    <![CDATA[A forest node
        This is the text of the forest node.]]>
    </data>
    <tree
        name="A tree node"
        guid="768eae66-e9df-4999-b950-01fa9be1a5cf"
        created="5/14/2004 1:05:38 AM"
        updated="5/14/2004 1:06:11 AM">
        <data>
        <![CDATA[A tree node
            This is the text of the tree node.]]>
        </data>
        <branch
            name="A branch node"
            guid="be4b0993-d4e4-4249-8aa5-fa9c940ae2be"
            created="5/14/2004 1:06:00 AM"
            updated="5/14/2004 1:06:24 AM">
            <data>
            <![CDATA[A branch node
                This is the text of the branch node.]]></data>
                <leaf
                name="A leaf node"
                guid="9c76ff4e-3ae2-450e-b1d2-232b687214aa"
                created="5/14/2004 1:06:26 AM"
                updated="5/14/2004 1:06:38 AM">
                <data>
                <![CDATA[A leaf node
                    This is the text of the leaf node.]]>
                </data>
            </leaf>
        </branch>
    </tree>
</forest>
</forestPad>

2 个答案:

答案 0 :(得分:7)

嗯,如果你的元素层次结构更像是......

会更容易
<node type="forest">
    <node type="tree">
        ...

...而不是您当前的架构。

按原样,您需要4个HierarchicalDataTemplate个,每个分层元素一个,包括根,一个DataTemplateleaf元素:

<Window.Resources>
    <HierarchicalDataTemplate
        DataType="forestPad"
        ItemsSource="{Binding XPath=forest}">
        <TextBlock
            Text="a forestpad" />
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate
        DataType="forest"
        ItemsSource="{Binding XPath=tree}">
        <TextBox
            Text="{Binding XPath=data}" />
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate
        DataType="tree"
        ItemsSource="{Binding XPath=branch}">
        <TextBox
            Text="{Binding XPath=data}" />
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate
        DataType="branch"
        ItemsSource="{Binding XPath=leaf}">
        <TextBox
            Text="{Binding XPath=data}" />
    </HierarchicalDataTemplate>
    <DataTemplate
        DataType="leaf">
        <TextBox
            Text="{Binding XPath=data}" />
    </DataTemplate>

    <XmlDataProvider
        x:Key="dataxml"
        XPath="forestPad" Source="D:\fp.xml">
    </XmlDataProvider>
</Window.Resources>

您可以通过编程方式设置Source的{​​{1}}:

XmlDataProvider

此外,重新保存您的修改就像这样简单:

dp = this.FindResource( "dataxml" ) as XmlDataProvider;
dp.Source = new Uri( @"D:\fp.xml" );

dp.Document.Save( dp.Source.LocalPath ); 本身需要TreeViewName绑定到ItemsSource

XmlDataProvider

在这个例子中,我在每个节点上<TreeView Name="treeview" ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}"> TwoWay es绑定,但是当它在一个单独的TextBox或其他节点中一次只编辑一个节点时控制,您将它绑定到TextBox的当前选定项目。您还可以将上述TreeView更改为TextBox,因为点击TextBlock实际上并未选择相应的TextBox

TreeViewItem

您必须使用两个<TextBox DataContext="{Binding ElementName=treeview, Path=SelectedItem}" Text="{Binding XPath=data, UpdateSourceTrigger=PropertyChanged}"/> 的原因是您不能同时使用BindingPath

修改

Timothy Lee Russell询问如何将CDATA保存到数据元素中。首先,在XPathInnerXml上进行一点。

在幕后,InnerText正在使用XmlDataProvider,其XmlDocument树。当诸如“stuff”之类的字符串被分配给XmlNodes的{​​{1}}属性时,那些标签就是标签。获取或设置InnerXml时不会进行转义,并将其解析为XML。

但是,如果将其分配给XmlNode属性,则尖括号将使用实体&amp; lt;和&amp; gt;。当值被重新检索时,会发生相反的情况。实体(例如&amp; lt;)被解析回字符(例如&lt;)。

因此,如果我们存储在数据元素中的字符串包含XML,则实体已被转义,我们需要通过在将CDATA部分添加为节点的子节点之前检索InnerXml来撤消它...

InnerText

如果节点已经有一个CDATA部分并且该值没有以任何方式改变,那么它仍然有一个CDATA部分,我们基本上用相同的替换它。但是,通过我们的绑定,如果我们更改数据元素内容的值,它将替换CDATA以支持转义字符串。然后我们必须解决它们。

答案 1 :(得分:2)

我们遇到了类似的问题。您可能会发现阅读this article很有帮助。我们使用了描述的ViewModel模式,它简化了所有内容。