Dynamically updating treeview node wpf
This blog entry demonstrates the fundamentals of binding a WPF Tree View to a Data Set with two related Data Tables.The technique presented herein could easily be extended to fit more sophisticated requirements, such as binding to more than two tables.For every item there has to be a node which has references to both the parent and child nodes, and these have to be maintained as changes are received.The big challenge is twofold: My first solution was to create a node which applied a filter to find its children and in turn each child also applied a filter to find its children.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 Tree View Items against the master Data Table, and then bind against Data Relations for any descendants of the root nodes.This method discards modifications to data since the last save.The Preview Edit Started event occurs when the treeview item is about to be edited.
In order to cancel the edit process in the code-behind you need to invoke the Cancel Edit() method of the editing treeview item.
Here is the XAML for a Window which contains a Tree View configured to load and display that data: If you had, say, three related tables (Master – Detail Info) then you could have the ‘Detail Template’ be a Hierarchical Data Template whose Items Source was bound to the Data Relation between ‘Detail’ and ‘Detail Info,’ and the Item Template a Data Template which displays the pertinent information in that table.
When you run the demo application and expand the root nodes, the Tree View looks like this: Click here to download the demo project.
I quickly identified the need for a new ‘convert a stream to a tree’ operator so I took the challenge to implement it.
However the more I looked into the problem the bigger the challenge become and I started to doubt whether I could pull it off.