[英]Two-way binding of Xml data to the 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>
好吧,如果您的元素层次结构更像是......
<node type="forest">
<node type="tree">
...
...而不是你当前的架构。
按HierarchicalDataTemplate
,您将需要4个HierarchicalDataTemplate
,一个用于包含根的每个分层元素,另一个用于leaf
元素的DataTemplate
:
<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>
您可以通过编程方式设置XmlDataProvider
的Source
:
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
es进行了TwoWay
绑定,但是当在单独的单个TextBox
或其他控件中一次编辑一个节点时,您将它绑定到TreeView
的当前所选项目。 您还可以将上面的TextBox
es更改为TextBlock
,因为在TextBox
中单击并不会实际选择相应的TreeViewItem
。
<TextBox
DataContext="{Binding ElementName=treeview, Path=SelectedItem}"
Text="{Binding XPath=data, UpdateSourceTrigger=PropertyChanged}"/>
您必须使用两个Binding
的原因是您不能同时使用Path
和XPath
。
编辑:
Timothy Lee Russell询问如何将CDATA保存到数据元素中。 首先,关于InnerXml
和InnerText
。
在幕后, XmlDataProvider
正在使用XmlDocument
,它的XmlNodes
树。 当诸如“stuff”的字符串被分配给XmlNode
的InnerXml
属性时,那些标记实际上是标记。 获取或设置InnerXml
时不会进行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以支持转义字符串。 然后我们必须解决它们。
我们遇到了类似的问题。 您可能会发现阅读本文很有帮助。 我们使用了描述的ViewModel模式,它简化了一切。
我知道这是一篇旧文章,但有一个更优雅的解决方案。 如果您使用 XPath 表达式选择您希望模板使用的所有节点,您确实可以使用单个HierarchicalDataTemplate
: XPath=tree|branch|leaf
。
<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>
这将呈现为:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.