【问题标题】:Two-way binding of Xml data to the WPF TreeViewXml 数据到 WPF TreeView 的双向绑定
【发布时间】:2010-09-16 07:46:53
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

    标签: c# wpf xml data-binding treeview


    【解决方案1】:

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

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

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

    按原样,您需要 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>
    

    您可以改为以编程方式设置XmlDataProviderSource

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

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

    dp.Document.Save( dp.Source.LocalPath );
    

    TreeView 本身需要一个Name 和一个绑定到XmlDataProviderItemsSource

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

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

    <TextBox
        DataContext="{Binding ElementName=treeview, Path=SelectedItem}"
        Text="{Binding XPath=data, UpdateSourceTrigger=PropertyChanged}"/>
    

    你必须使用两个Bindings的原因是你不能同时使用PathXPath

    编辑:

    Timothy Lee Russell 询问有关将 CDATA 保存到数据元素的问题。首先,介绍一下InnerXmlInnerText

    在幕后,XmlDataProvider 使用XmlDocument,它的树是XmlNodes。当将诸如“stuff”之类的字符串分配给XmlNodeInnerXml 属性时,这些标签就是真正的标签。获取或设置InnerXml时不进行转义,解析为XML。

    但是,如果将其分配给InnerText 属性,则尖括号将使用实体 < 进行转义。和>。取回值时会发生相反的情况。实体(如 <)被解析回字符(如

    因此,如果我们存储在数据元素中的字符串包含 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 以支持转义字符串。然后我们必须修复它们。

    【讨论】:

    • 谢谢乔尔,这行得通。一个问题。我用 CDATA 部分围绕数据元素中的内容,以便可以存储 Xml。有没有办法控制 XmlDataProvider 如何写出数据元素?
    • 如果有 XML 作为字符串,它将用实体转义尖括号(它们以 & 开头)。这可以逆转,因为 Document 属性返回一个 XmlDocument。我将在数据元素中编辑并添加用于执行 CDATA 的代码。
    • 太棒了——行得通。对于我正在处理的文档的大小,性能确实很差,但我不会更新每个节点,而是添加一个 IsDirty 标志,并且只更新已编辑的节点。
    • 我也不想要根节点,所以我将 TreeView 的 ItemsSource 中的 XPath 更改为“XPath=forest”而不是“XPath=”。效果很好。
    • 我意识到这是一个非常古老的问答,但是今天这个答案对我帮助很大,所以我想谢谢你。不过,我很好奇,如果 XML 是 node 树,如您所建议的,解决方案会有什么不同?
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      这是一个完整的 Page 示例,其中 XData 嵌入在 XmlDataProvider1 中:

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

      这将呈现为:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-09
        • 1970-01-01
        • 2017-01-02
        • 1970-01-01
        • 2010-09-19
        • 2011-09-12
        • 2016-03-01
        • 2010-10-24
        相关资源
        最近更新 更多