WPF TreeView Update
April 7th, 2008
I’ve posted an update for my WPF TreeView which contains a bugfix and two new features:
- The root item collection is now monitored for changes, and the tree updates itself automatically. This behaviour, however, can be controlled through the ObserveRootItems dependency property.
- Built-in filtering support through a strongly typed predicate. I’m not completely happy with my implementation though – as a matter of fact, I’ve already started to rewrite it completely – you can expect the next version within the next 10 days. The filtering API however, will remain intact.
In case you did override some of the tree’s virtual methods, your project might not compile out of the box because some of these methods now receive additional parameters. However, as nothing has been removed, adjusting your code should be a matter of seconds.
I’ve added the download link to the original post:
http://www.hardcodet.net/2008/01/wpf-treeview
Happy coding 🙂
Hello Philipp,
Congratulations for your tool … it’s very nice !!!
I am looking for some advices : I’d like to use your tree to create a hierarchical database explorer (enter data, see data, sort data …). The database is in sql server. What are your suggestions to do that ?
My main problem concerns the creation of the ObservableColection (from a SQL table) which is mandatory to use your tree. How can I ensure binding between sql/ObservableCollection/tree
Thank you for your time.
Rodolphe
Hi Rodolphe,
You don’t have to use an ObservableCollection – every collection that implements the ICollection interface is ok. However, the tree operates strictly object oriented – if you are working upon relational data, you’ll have to perform some kind of object-relational mapping (mapping of your data on objects, e.g. by writing a TableInfo class that provides information about a given table):
table row -> object -> tree
In WPF, a pattern called Model-View-ViewModel is widely adpted. Basically, it means that you provide a convenient object model that plays nicely with your WPF interface:
http://www.google.com/search?q=wpf%20viewmodel
Hope that helps – happy coding 🙂
Philipp
@Philipp Sumi
thanks for our tips.
Hi,
I just want to know if there is an easy way to set/change the image dynamically in the code behind for some nodes.
Thank you.
Hi Philipp,
have you implemented the built-in filtering and multi-selection support? If yes, will you post an update 🙂
@xenry
I’m afraid there’s just no time for that. For now, I’ll only provide bug fixes, should they be required, I’m afraid.
I’m sorry I don’t have better news for you :/
Cheers,
Philipp
Very Nice!!!
New to the WPF world but I have a lot of win forms experience. I’ve been review your code and am trying to add a Multi-Select functionality with no success.
WinForms extending the control seems easier – I must have a bias.
Why didn’t you just Extend the TreeView – only asking because I’m a newbie.
With your code – I’ve added a List SelectedItems collection and added the following logic to the OnSelectedItemPropertyChanged method:
bool multiselect = ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)));
…
try
{
if (!multiselect)
SelectedItems.Clear();
SelectedItems.Add(newValue);
SelectedItems.ForEach(SelectItemNode);
}
…
The selected item changes but I loose the visual “Selected Item” from the previous selection…
Any thoughts?
Thanks for any help.