Return to site

Wpf Treeview Binding

broken image


The above code produces the following result: In the above code, the top level items source is a list of CelebrityCategory objects. However because the TreeView's ItemTemplate is a HierarchicalDataTemplate and the HierarchicalDataTemplate's ItemsSource is bind to the CelebrityCategory's Celebrities property (which provides a list of celebrities for that category), each top level item is.

Treeview

In a new WPF application I'm writing I needed to bind the 'SelectedItem'property of the TreeView control to the 'SelectedItem' property of itsViewModel; but, alas, the TreeView's 'SelectedItem' property isread-only...

  1. Bind RadTreeView ImageSource Properties RadTreeView API gives you the ability to define images for each item state (default, expanded and selected). For more information, take.
  2. TreeView Styles and Templates.; 4 minutes to read; A; In this article. This topic describes the styles and templates for the TreeView control. You can modify the default ControlTemplate to give the control a unique appearance. For more information, see Create a template for a control.

After some googling I found the Versatile TreeView by Philip Sumi whichadds the property I wanted, but since I'm learning WPF and want a deeperunderstanding I decided to write my own (and use Philip's TreeView asreference).

In the code I noticed a lot of dependency properties, and I figured its time tolearn what are dependency properties and how do they work; again, some googlingbrought me to this post which I think gives a very good explanation ofdependency properties.

Now that I knew what I had to do, I started writing the EnhancedTreeView class(which is based on the TreeView class).

At first I thought about overriding the SelectedItem property but it didn't govery smooth so I added the SelectedObject property.

Wpf Treeview Binding Tutorial

I added a callback to the dependency-property-changed event (seeSelectedObjectChangedCallback) and when the property was changed I needed tochange the selected item; but, alas again!, the selected item isread-only...

Some more googling brought me to this blog post which uses theItemsContainerGenerator to find the matching TreeViewItem for the object Iwant to select.

And here's the final code:

To use the EnhancedTreeView all you need is to bind the SelectedObjectproperty:

It's important to add Mode=TwoWay to the binding, otherwise it won't work.

Wpf Treeview Binding Example

This blog entry demonstrates the fundamentals of binding a WPF TreeView to a DataSet with two related DataTables. The technique presented herein could easily be extended to fit more sophisticated requirements, such as binding to more than two tables.

Many applications need to display hierarchical data in a TreeView, and often that data is retrieved from a database. In many situations the developer just needs to bind the TreeView directly to the DataSet which was populated with database data; creating custom domain objects and collections of those objects can be overkill sometimes. If you are currently in that situation, rest assured that it is actually fairly trivial to do this in WPF. 🙂

The basic gist of the solution is to bind the top level of TreeViewItems against the master DataTable, and then bind against DataRelations for any descendants of the root nodes. You need to use a HierarchicalDataTemplate for every non-leaf level of nodes, in other words, only the very lowest DataTable in the hierarchy is displayed with a non-hierarchical DataTemplate.

Let's just get right into an example. Here is a method in a class called DataSetCreator which creates a DataSet with two related DataTables:

The resultant DataSet has two DataTables (‘Master' and ‘Detail') and one DataRelation (‘Master2Detail'). We want a TreeView to display the Master rows as top-level nodes and the Detail rows as children of their respective parent node. Here is the XAML for a Window which contains a TreeView configured to load and display that data:

If you had, say, three related tables (Master –> Detail –> DetailInfo) then you could have the ‘DetailTemplate' be a HierarchicalDataTemplate whose ItemsSource was bound to the DataRelation between ‘Detail' and ‘DetailInfo,' and the ItemTemplate a DataTemplate which displays the pertinent information in that table.

When you run the demo application and expand the root nodes, the TreeView looks like this:

Wpf Treeview Binding Hierarchicaldatatemplate

Click here to download the demo project. Be sure to change the file extension from DOC to ZIP and then unzip it.





broken image