将Xml数据双向绑定到WPF TreeView

Tim*_*ell 4 c# xml data-binding wpf 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>
Run Code Online (Sandbox Code Playgroud)

Joe*_*ant 7

好吧,如果您的元素层次结构更像是......

<node type="forest">
    <node type="tree">
        ...
Run Code Online (Sandbox Code Playgroud)

...而不是你当前的架构.

AS-是,你需要4个HierarchicalDataTemplateS,一个包括根各层次元素,以及一个DataTemplate用于leaf元素:

<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>
Run Code Online (Sandbox Code Playgroud)

您可以改为SourceXmlDataProvider编程方式设置:

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

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

dp.Document.Save( dp.Source.LocalPath );
Run Code Online (Sandbox Code Playgroud)

TreeView本身需要一个Name和一个ItemsSource粘合到XmlDataProvider:

<TreeView
    Name="treeview"
    ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}">
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我在每个节点上都TwoWay使用TextBoxes进行绑定,但是当在单独的,单个TextBox或其他控件中一次只编辑一个节点时,您将它绑定到当前所选的项目TreeView.您还可以将上面的TextBoxes 更改为TextBlocks,因为单击中TextBox并不会实际选择相应的TreeViewItem.

<TextBox
    DataContext="{Binding ElementName=treeview, Path=SelectedItem}"
    Text="{Binding XPath=data, UpdateSourceTrigger=PropertyChanged}"/>
Run Code Online (Sandbox Code Playgroud)

您必须使用两个原因Bindings是,你不能使用PathXPath在一起.

编辑:

Timothy Lee Russell询问如何将CDATA保存到数据元素中.首先,一点点InnerXmlInnerText.

在幕后,XmlDataProvider正在使用XmlDocument它的树XmlNodes.当诸如"stuff"的字符串被分配给a的InnerXml属性时XmlNode,那些标签就是标签.获取或设置时不进行转义InnerXml,并将其解析为XML.

但是,如果将其分配给InnerText属性,则尖括号将使用实体< 和>.当值被重新检索时,会发生相反的情况.实体(如<)被解析回字符(如<).

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

XmlDocument doc = dp.Document;

XmlNodeList nodes = doc.SelectNodes( "//data" );

foreach ( XmlNode node in nodes ) {
    string data = node.InnerText;
    node.InnerText = "";
    XmlCDataSection cdata = doc.CreateCDataSection( data );
    node.AppendChild( cdata );
}

doc.Save( dp.Source.LocalPath );
Run Code Online (Sandbox Code Playgroud)

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