0

我已经在微软论坛上问过这个问题,但直到现在都没有答案。我被困在这里。我有一个非常嵌套的 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>

我的分层数据模板如下所示:

<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 读取结构,但我会失去模板的所有优点。

4

2 回答 2

0

我将反序列化您的 xml,然后使用以下项目的自定义树结构:

http://www.codeproject.com/KB/WPF/versatile_treeview.aspx

我有一个类似的问题(如何将自定义 XML 转换为树视图),我发现这个解决方案效果很好。

于 2009-05-18T22:01:54.447 回答
0

在您的“产品”模板中,您只需要将 inifile 作为子级调用:

ItemsSource="{Binding XPath=inifiles/inifile}"

您需要更改此设置,以便您还需要子产品。就像是:

ItemsSource="{Binding XPath=inifiles/inifile|subproducts}"

您需要检查确切的 XPath 语法——我没有对此进行测试。

于 2009-08-03T13:42:19.243 回答