0

I am having trouble understanding how to Bind Data to a WPF TreeView when custom objects are used. I have researched and watched tutorials but I am still stuck.

BACKGROUND: consider three classes (I have simplified my problem to this). They represent a database which has a Table, each table can have Fields. There are a list of Tables.

1) TableList Class With Property

List<Table Objects> 

2) Table Class:

  • Property Name
  • With a TableFields Property

    SortedDictionary <Name, Field Object>
    

3) Field Class:

  • With a Name Property

An example of my current attempt to bind a field (lowest level) to Table.TableFields.Key

<DataTemplate x:Key="fieldTemplate">
            <TextBlock Text="{Binding Path=Table.TableFields.Key}"/>
</DataTemplate>

DESIRED OUTPUT - a hierarchical view of the table list, containing tables and their fields.

Table 1
   Field 1
   Field 2
   Field 3
Table 2
   Field 1
   Field 2
   Field 3
 Table N
   Field N
  • I am after guidance so I can better understand how to bind this data, an example of my issue is the data binding has to look at a Table Objects TableFields property which is a SortedDictionary, in which I want to get the Key which will be a Field Name.

I am confused with how to bind custom objects and access the information like this.

2
  • I'm not sure I'm understanding the problem, what I've always used when working with tree visualizations is creating a Node class that is the one bound to the tree control and is a tree friendly data class and it contains the data of the various objects composing the levels of the tree created by a loading method reading the data. Commented Mar 7, 2016 at 9:04
  • @sabrina_cs I updated my question Commented Mar 8, 2016 at 4:32

1 Answer 1

1

This will display your TreeView

      <TreeView ItemsSource="{Binding TableList}">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource = "{Binding Path=TableFields}">
                    <TextBlock Text="{Binding Path=Name}"/>
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Key}"/>
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for replying, however I am not sure this helps. I am more after understanding and learning how to correctly do this. I also dont think that HierarchialDataTemplate to TableFields will work? I would not have any reference to the Tables themselves in this case. Updated by adding my expected output.
Whatever collection you set as ItemsSource of treeView, will be displayed by HierarchicalDataTemple which is same as DataTemplate but in addition it provides ItemsSource property which you can set to show sub levels. HierarchicalDataTemplate.ItemTemplate will define how that sub-level will be shown. Similarly using HierarchicalDataTemplate within HierarchicalDataTemplate you can show as many sublevel as u want.
I have added an improved question to hopefully assist with understanding what I am trying to do. I am working on this side project to get back into programming. Your help would be greatly appreciated so I can learn this concept and apply it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.