tags:

views:

1705

answers:

2

Dear ladies and sirs.

Does anyone know of a good implementation of a WPF TreeView with multiple selection?

I am currently aware of one commercial implementation - http://www.telerik.com/products/wpf/treeview.aspx

My employer has no problem spending money on a commercial product, so I would like to know more of good commercial implementations.

Thanks.

+1  A: 

I've written a blog post on how to implement this on the WPF tree view control,

Implementing Multi Select behavior on the WPF Tree View Control

mattdlong
A: 

Depending on the exact semantics you desire, the solution may be extremely simple:

If the root of your tree is anything but a TreeView -- for example if it is a plain ItemsControl -- all TreeViewItems in the tree will be independently selectatble so you basically get mulitiselect for free. So just use an ItemsControl instead of a TreeView for the root of your tree.

This solution has the benefit of being extraordinarily simple to implement. It differs from mattdlong's solution in that:

  • His solution deselects all other items when an item is clicked, so you have to ctrl-click items to multiselect.
  • With this solution a single click will select/deselect the item you clicked on, but there is no way to quickly select an item and simultaneously deselect all others.

Another difference is that keyboard navigation (arrow keys) in his solution deselects all items, whereas in this solution keyboard navigation does not deselect items.

You should choose between these solutions based on the semantics you prefer (single click to add item vs ctrl-click to add item, etc). If you want more advanced semantics, such as Shift-Click, etc, it is relatively to add.

Note that you can also custom style TreeViewItems using a ToggleButton or CheckBox anywhere in the ItemContainerTemplate that has Checked={Binding IsSelected}. This allows the user to select items by clicking on the ToggleButton or CheckBox.

Ray Burns
I have some experience with trees in UI to learn one rule - there are so many details in implementing multiple selection correctly, that I really do not wish to take this road. I prefer someone, whose business is designing UI controls and who has already invested in testing and tuning, rather than producing something quick, which will constantly draw resources to fix and maintain. It is nice as a programming exercise, granted.
mark
I think you misunderstood my answer. What I was trying to say is that TreeViewItem supports simple multiselection out of the box, so **you don't have to write anything at all.** If you like its built-in multiselection semantics you can just use it. If you want something different than the built-in multiselect behavior, **then** you'll have to either buy a control or write code like mattdlong describes.
Ray Burns