Click here to Skip to main content
15,881,559 members
Articles / Desktop Programming / WPF

WPF MultiSelect TreeView Sample

Rate me:
Please Sign up or sign in to vote.
4.63/5 (11 votes)
23 Mar 2008CPOL2 min read 151.2K   6.2K   30   18
Shows how to add multi-select capabilities to WPF TreeView control

MultiSelectTreeView

Note - this is one of my earliest articles and I cannot guarantee that the sample is working and the presentation is clear. I plan to write another article that will incorporate the samples from my earliest articles making them work. Thanks for your understanding. 

Introduction

WPF TreeView control does not give developers an ability to multi-select the nodes of the tree. This article shows a very simple example that simulates multi-select capability for a WPF TreeView control. I say "simulate" because it discards the WPF select mechanism and replaces it with its own.

Ideally another user control called, say MultiSelectTreeView, should be created and packed in a separate DLL, but since I do not have much time to spend on it, I simply showed how to do it by modifying the code in the main Window class.

To multi-select, you should use the left ctrl key on your keyboard together with the mouse. The shift key and ctrl-A multi-select functions were not added (again for the lack of time), but can be added in exactly the same manner.

Simulating the Multi-Selection

As stated above, we disable the WPF selection and, instead use our own selection mechanism, according to which, all TreeViewItem elements that have been selected are stored in selectedItems set. The set is simulated by a Dictionary with null values.

The selected items change their Background and Foreground properties to Brushes.Black and Brushes.White correspondingly.

If LeftCtrl key is not pressed, then, before selecting an item, we clean all the previous selections. If LeftCtrl key is pressed, the previous selections are not cleaned.

Using the Code

The top level code is located in MyTreeView_SelectedItemChanged function and called when SelectedItemChanged event is fired on the TreeView.

First, prevent WPF selection:

C#
treeViewItem.IsSelected = false;

Then, if LeftCtrl is not pressed, clear the previous selections:

C#
if (!CtrlPressed)
{
    List<treeviewitem> selectedTreeViewItemList = new List<treeviewitem>();
    foreach (TreeViewItem treeViewItem1 in selectedItems.Keys)
    {
        selectedTreeViewItemList.Add(treeViewItem1);
    }

    foreach (TreeViewItem treeViewItem1 in selectedTreeViewItemList)
    {
        Deselect(treeViewItem1);
    }
}</treeviewitem></treeviewitem>

Then, flip flop the selection state of the item: if selected, make it unselected and vice versa:

C#
ChangeSelectedState(treeViewItem);

We check whether LeftCtrl key is pressed or not by implementing the following property:

C#
bool CtrlPressed
{
   get
   {
       return System.Windows.Input.Keyboard.IsKeyDown(Key.LeftCtrl);
   }
}

History

  • 23rd March, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect AWebPros
United States United States
I am a software architect and a developer with great passion for new engineering solutions and finding and applying design patterns.

I am passionate about learning new ways of building software and sharing my knowledge with others.

I worked with many various languages including C#, Java and C++.

I fell in love with WPF (and later Silverlight) at first sight. After Microsoft killed Silverlight, I was distraught until I found Avalonia - a great multiplatform package for building UI on Windows, Linux, Mac as well as within browsers (using WASM) and for mobile platforms.

I have my Ph.D. from RPI.

here is my linkedin profile

Comments and Discussions

 
PraiseAwesome Pin
Member 1262015624-Aug-23 10:45
Member 1262015624-Aug-23 10:45 
GeneralRe: Awesome Pin
Nick Polyak24-Aug-23 12:47
mvaNick Polyak24-Aug-23 12:47 
GeneralMy vote of 3 Pin
Chougule Vinay10-May-12 4:56
Chougule Vinay10-May-12 4:56 
SuggestionSimpler and Improved Version Pin
kessbest13-Jul-11 6:04
kessbest13-Jul-11 6:04 
GeneralRe: Simpler and Improved Version Pin
Nick Polyak13-Jul-11 13:40
mvaNick Polyak13-Jul-11 13:40 
GeneralRe: Simpler and Improved Version Pin
PShchegolevatykh28-Mar-12 23:15
PShchegolevatykh28-Mar-12 23:15 
GeneralMake MS notice Pin
Member 778244829-Mar-11 21:08
Member 778244829-Mar-11 21:08 
Generaldecent article Pin
BillW336-Jan-11 8:22
professionalBillW336-Jan-11 8:22 
QuestionHow to bind the data from database to treeview Pin
Jasmine Pomelo13-May-09 18:51
Jasmine Pomelo13-May-09 18:51 
AnswerRe: How to bind the data from database to treeview Pin
Nick Polyak14-May-09 2:14
mvaNick Polyak14-May-09 2:14 
GeneralA good starting point. but ... Pin
tabor2530-Apr-08 2:18
tabor2530-Apr-08 2:18 
GeneralRe: A good starting point. but ... [modified] Pin
Nick Polyak30-Apr-08 2:48
mvaNick Polyak30-Apr-08 2:48 
GeneralRe: A good starting point. but ... Pin
tabor251-May-08 6:16
tabor251-May-08 6:16 
GeneralRe: A good starting point. but ... Pin
Nick Polyak1-May-08 15:12
mvaNick Polyak1-May-08 15:12 
GeneralRe: A good starting point. but ... Pin
otbld_simon9-Aug-09 21:21
otbld_simon9-Aug-09 21:21 
Did you find a solution yet?
GeneralRe: A good starting point. but ... Pin
Nick Polyak10-Aug-09 5:09
mvaNick Polyak10-Aug-09 5:09 
AnswerRe: A good starting point. but ... Pin
jan.mudrak@gmail.com6-Dec-11 1:02
jan.mudrak@gmail.com6-Dec-11 1:02 
GeneralRe: A good starting point. but ... Pin
Nick Polyak6-Dec-11 15:22
mvaNick Polyak6-Dec-11 15:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.