在前面说我对WPF比较陌生。
因此,基本上我得到了以下XML,我需要将树元素和下面的所有内容绑定到TreeView。有没有一种简单的方法可以将特定的XML元素绑定到TreeView?
<Diff>
<Packages
left="c:foofoo"
right="c:barbar" />
<Section name="Filesystem" state="different">
<Tree>
<Node name="VFS" state="different">
<Node name="Windows" state="different">
<Node name="System32" state="different">
<Leaf name="notepad.exe" state="left-only" file="VFSWindowsSystem32notepad.exe" />
<Leaf name="cmd.exe" state="same" />
</Node>
<Node name="WinSxS" state="same">
<Leaf name="foo" state="same" />
</Node>
</Node>
<Node name="ProgramFilesX86" state="different">
<Leaf name="foo.exe" state="different" />
<Node name="Bar" state="right-only" >
<Leaf name="bar.exe" state="right-only" file="VFSProgramFilesX86Barbar.exe" />
</Node>
</Node>
</Node>
</Tree>
</Section>
<Section>
<!--Another tree-->
</Section>
</Diff>
也不确定这是否重要,但请注意,可能有多个section元素,每个元素下面都有一个树。
假设使用XmlDataProvider
将控件绑定到XML,则应该能够设置XPath
属性来过滤结果。
<XmlDataProvider
x:Key="FilesystemXmlData"
Source="source.xml"
XPath="Section[@name='Filesystem']/Tree"
/>
然后,您可以使用HierarchicalDataTemplate
将数据绑定到树。
这篇CodeProject文章中有一个简单的例子。