WPF TreeView的Xml数据双向绑定

4
我尝试重新编写我的ForestPad应用程序,利用WPF作为呈现层。在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>
3个回答

9

如果您的元素层次结构更像这样,那么问题会更容易解决...

<node type="forest">
    <node type="tree">
        ...

...而不是您当前的模式。

目前,您需要4个HierarchicalDataTemplate,每个包括根层次结构元素,以及一个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>

你可以通过编程的方式设置 XmlDataProviderSource
dp = this.FindResource( "dataxml" ) as XmlDataProvider;
dp.Source = new Uri( @"D:\fp.xml" );

此外,保存您的编辑非常简单:
dp.Document.Save( dp.Source.LocalPath );

TreeView本身需要一个Name和一个与XmlDataProvider绑定的ItemsSource

<TreeView
    Name="treeview"
    ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}">

在这个例子中,我使用每个节点上的TextBox 进行TwoWay绑定。但是,如果要在单独的单个TextBox或其他控件中仅编辑一个节点时,您将把它绑定到TreeView 的当前选定项。您还需要将上述TextBox更改为TextBlock,因为单击TextBox实际上不会选择相应的TreeViewItem
<TextBox
    DataContext="{Binding ElementName=treeview, Path=SelectedItem}"
    Text="{Binding XPath=data, UpdateSourceTrigger=PropertyChanged}"/>

你必须使用两个Binding的原因是因为你不能同时使用PathXPath
编辑:
Timothy Lee Russell问如何将CDATA保存到数据元素中。首先,介绍一下InnerXmlInnerText
在幕后,XmlDataProvider使用一个XmlDocument,它有一个XmlNodes树。当将字符串(例如“stuff”)分配给XmlNodeInnerXml属性时,那些标记实际上是标记。获取或设置InnerXml时不会进行任何转义,并且它会被解析为XML。
但是,如果将其分配给InnerText属性,则尖括号将用实体&lt;和&gt;转义。当检索该值时,相反的情况发生。实体(如&lt;)被解析回字符(如<)。
因此,如果我们存储在数据元素中的字符串包含XML,则实体已被转义,我们需要通过在添加CDATA节作为节点的子节点之前检索InnerText来撤消这一点...
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 );

如果节点已经有了一个CDATA部分,并且值没有被改变,那么它仍然具有CDATA部分,我们基本上用相同的内容替换它。然而,通过我们的绑定,如果我们更改了数据元素内容的值,它会用转义字符串替换CDATA。然后我们就需要修复它们。

谢谢Joel,它有效。不过我有一个问题。我用CDATA部分包围数据元素中的内容,以便可以存储Xml。是否有一种方法来控制XmlDataProvider如何写出数据元素? - Timothy Lee Russell
如果有XML字符串,它会使用实体转义角括号(它们以&开头)。这可以通过Document属性返回XmlDocument来反转。我将编辑并添加代码以在数据元素中执行CDATA。 - Joel B Fant
我意识到这是一个非常古老的问题和答案,但是今天这个回答帮了我很多,所以我想感谢你。然而,如果XML是node树,那么解决方案会有什么不同呢? - Naikrovek
在文章跳转后描述了(好或坏的)理由。与许多决策一样,它有些武断。在我目前使用ASP.NET WebAPI和AngularJS构建的ForestPad网络版本中,我对每个具有无限深度的节点使用相同的数据类型。旧版ForestPad文章链接:http://www.codeproject.com/Articles/7255/ForestPad-a-method-for-storing-and-retrieving-text - Timothy Lee Russell
实际上,您只需要使用一个HierarchicalDataTemplate。您只需要使用选择要使用的所有节点的XPath表达式:XPath = tree | branch | leaf - B. Fuller
显示剩余3条评论

2
我们曾遇到类似的问题。你可以阅读这篇文章,它可能会对你有所帮助。我们采用了描述的ViewModel模式,使一切变得简单化。

1

我知道这是一个老帖子,但有一个更优雅的解决方案。如果您使用选择所有要使用模板的节点的XPath表达式XPath=tree|branch|leaf,则确实可以使用单个HierarchicalDataTemplate

<HierarchicalDataTemplate x:Key="forestTemplate"
        ItemsSource="{Binding XPath=tree|branch|leaf}">
    <TextBlock Text="{Binding XPath=data}" />
</HierarchicalDataTemplate>

这是一个完整的具有嵌入在XmlDataProvider1中的XData的Page示例:
<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <XmlDataProvider x:Key="sampleForestPad" XPath="forestPad/forest">
        <x:XData>
            <forestPad xmlns=""
                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>
        </x:XData>
    </XmlDataProvider>

    <HierarchicalDataTemplate x:Key="forestTemplate"
        ItemsSource="{Binding XPath=tree|branch|leaf}">
      <TextBlock Text="{Binding XPath=data}" />
    </HierarchicalDataTemplate>

    <Style TargetType="TreeViewItem">
      <Setter Property="IsExpanded" Value="True"/>
    </Style>
  </Page.Resources>

    <TreeView ItemsSource="{Binding Source={StaticResource sampleForestPad}}"
      ItemTemplate="{StaticResource forestTemplate}"/>
</Page>

这将被渲染为:

ForestTreeView


有趣,谢谢!下次我需要处理它时,我会尝试一下。 - Timothy Lee Russell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接