我已经在微软论坛上问了这个问题,但直到现在才回答。我被困在这里。我有一个非常嵌套的xml片段,我喜欢通过分层数据模板进行绑定。
以下是xml片段:
<project>
<products>
<product name="Product2" foldername="string" dbkey="-2405" dbtable="string">
<inifiles>
<inifile name="string" dbkey="-3083" dbtable="string">
<sections>
<section name="string" dbkey="-3025" dbtable="string">
<inientries>
<inikey name="string" value="string" dbkey="9739" dbtable="string" />
</inientries>
</section>
</sections>
</inifile>
</inifiles>
<subproducts>
<subproduct dbkey="1644" dbtable="string" name="Subproduct1">
<inifiles>
<inifile name="string" dbkey="-6544" dbtable="string">
<sections>
<section name="string" dbkey="2436" dbtable="string">
<inientries>
<inikey name="string" value="string" dbkey="-2122" dbtable="string" />
</inientries>
</section>
</sections>
</inifile>
</inifiles>
</subproduct>
<subproduct dbkey="-4746" dbtable="string" name="Subproduct2">
<subinifiles>
<subinifile name="string" dbkey="7519" dbtable="string">
<subsections>
<subsection name="string" dbkey="1680" dbtable="string">
<subinientries>
<subinikey name="string" value="string" dbkey="3682" dbtable="string" />
</subinientries>
</subsection>
</subsections>
</subinifile>
</subinifiles>
</subproduct>
</subproducts>
</product>
`</products>
</project>
My Hierarchical Datatemplates看起来像这样:
<HierarchicalDataTemplate
DataType="product"
ItemsSource="{Binding XPath=inifiles/inifile}"
>
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16"
Source="Images/gnome-applications.png"/>
<TextBlock Text="{Binding XPath=@name}" FontWeight="bold"/>
</StackPanel>
</HierarchicalDataTemplate>
<!-- ######################### Ini-Files #########################################
-->
<HierarchicalDataTemplate
DataType="inifile"
ItemsSource="{Binding XPath=sections/section}"
x:Name="inifile"
>
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16"
Source="Images/advanced.png"/>
<TextBlock Text="{Binding XPath=@name}">
<TextBlock.ContextMenu>
<ContextMenu>
<Menu BorderThickness="3">
<MenuItem Header="{Binding XPath=@name}">
<MenuItem Header="_Find in Database"/>
<MenuItem Header="_Edit" Tag="{Binding XPath=@value}"/>
</MenuItem>
</Menu>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
<TextBlock Text="{Binding XPath=@key}"/>
</StackPanel>
</HierarchicalDataTemplate>
<!-- ######################### Sections #########################################
-->
<HierarchicalDataTemplate
DataType="section"
ItemsSource="{Binding XPath=inientries/inikey}">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16"
Source="Images/indent.png"/>
<TextBlock Text="{Binding XPath=@name}">
<TextBlock.ContextMenu>
<ContextMenu>
<Menu>
<MenuItem HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Height="Auto"
Width="Auto"
Header="{Binding XPath=@name}">
<MenuItem Header="_Find in Database"/>
<MenuItem Header="_Edit"
Tag="{Binding XPath=@value}"/>
</MenuItem>
</Menu>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
<TextBlock Text="{Binding XPath=@key}"/>
</StackPanel>
</HierarchicalDataTemplate>
<!-- ######################### Ini-Keys #########################################
-->
<HierarchicalDataTemplate
DataType="inikey">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16"
Source="Images/keyring.png"/>
<TextBlock Text="{Binding XPath=@name}">
<TextBlock.ContextMenu>
<ContextMenu>
<Menu>
<MenuItem HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Height="Auto"
Width="Auto"
Header="{Binding XPath=@name}">
<MenuItem Header="_Find in Database"/>
<MenuItem Header="_Edit"
Tag="{Binding XPath=@value}"
/>
</MenuItem>
</Menu>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
<TextBlock Text="{Binding XPath=@value}"/>
</StackPanel>
</HierarchicalDataTemplate>
我可以绑定除<subproducts>
标记之外的所有标记。我可以通过XmlDocument读取结构,但我会失去模板的所有优点。
答案 0 :(得分:0)
我会反序列化你的xml,然后使用以下项目的自定义树结构:
http://www.codeproject.com/KB/WPF/versatile_treeview.aspx
我遇到了类似的问题(如何将自定义XML提供给树视图),我发现这个解决方案非常有效。
答案 1 :(得分:0)
在您的“产品”模板中,您只是要求孩子们使用inifiles:
ItemsSource="{Binding XPath=inifiles/inifile}"
您需要更改此设置,以便您也可以调用子产品。类似的东西:
ItemsSource="{Binding XPath=inifiles/inifile|subproducts}"
您需要检查确切的XPath语法 - 我没有对此进行测试。