WPF Databinding More Complex XML Structure

I have asked this question already on the Microsoft forums, but still have not received an answer. I am stuck here. I have a fairly nested xml snippet that I like to bind using hierarchical data templates.

Here is the xml snippet:

<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 look like this:

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

      

I can bind to all tags except the tag <subproducts>

. I could read the structure through the XmlDocument, but I would lose all the benefits of templates.

0


source to share


2 answers


I would deserialize your xml and then use the following constructive project structure:

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



I had a similar problem (how to get custom XML in treeview) and found this solution to work fine.

0


source


In your "product" template, you only call inifiles as children:

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

      

You need to change this so that you also invoke offal. Something like:



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

      

You will need to check the exact XPath syntax - I haven't tested this.

0


source







All Articles