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
.