views:

1420

answers:

4

Hello, I asked this question already at the Microsoft forums, but no answer until now. I am stuck here. I have a quite nested xml snippet, which i like to bind via 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 for the <subproducts> tag. I could read the structure through an XmlDocument, but i would loose all the advantages of the templates. Thank you in advance.

A: 

First of all thank you for your answer.

The main problem here are the hierarchical data templates. I can bind to all elements of the xml file you saw above, except for the <subproducts> element and its child-elements.

I defined the following hierarchical template for <subproducts>:

<HierarchicalDataTemplate 
            DataType="subproducts"
                  >
            <StackPanel Orientation="Horizontal">

                <TextBlock Text="subproducts" FontWeight="bold"/>

            </StackPanel>
        </HierarchicalDataTemplate>


        <HierarchicalDataTemplate 
            DataType="subproduct"
             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>

And many other variations of the hierarchical templates for the <subproducts> tag.

But unfortunately none of them worked.

I am unsure because I don't know exactly, if i should use ItemTemplates inside the hierarchical templates to model the next nesting level or not. I am able to build the correct structure by reading the xml-file recursively and adding the nodes to the treeview, but I will losse all formatting and functionality of the elements provided by the datatemplates, because the rest of my code is built on making heavy use of datatemplates.

Hope this explains it better. thank you.

A: 

Hi there,

I don't know, why nobody answers. It would be nice to get only a hint or something.

Thank you in advance.

A: 

I would deserialize your xml and then use the following project's custom tree structure:

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

I had a similar issue (how to get custom XML to a treeview) and I've found this solution to work great.

Alex
A: 

In your "product" template you are only calling for inifiles as children:

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

You need to change this so that you also call for subproducts. Something like:

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

You would need to check the exact XPath syntax - I did not test this.