5,783,659 members and growing! (14,722 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Controls     Intermediate License: The Code Project Open License (CPOL)

WPF MultiSelect TreeView Sample

By Nick Polyak

Shows how to add multi-select capabilities to WPF TreeView control
C# (C# 3.0, C#), .NET (.NET, .NET 3.5, .NET 3.0), WPF, Design, Architect, Dev

Posted: 23 Mar 2008
Updated: 23 Mar 2008
Views: 12,662
Bookmarked: 10 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 2.32 Rating: 3.86 out of 5
0 votes, 0.0%
1
1 vote, 25.0%
2
0 votes, 0.0%
3
1 vote, 25.0%
4
2 votes, 50.0%
5
MultiSelectTreeView

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:

treeViewItem.IsSelected = false;

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

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

    foreach (TreeViewItem treeViewItem1 in selectedTreeViewItemList)
    {
        Deselect(treeViewItem1);
    }
}

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

ChangeSelectedState(treeViewItem);

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

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)

About the Author

Nick Polyak


I have 15 years of experience developing enterprise software, starting from C++ and Java on UNIX and moving towards C# on Windows platforms.
I am facinated by the new .NET technologies especially WPF, Silverlight 2.0 and LINQ.
Recently I decided to make a move and start my own contracting consulting and mentoring company AWebPros.
I can be contacted via my web site awebpros.com or through my blog at nickssoftwareblog.com
Occupation: Architect
Company: AWebPros
Location: United States United States

Other popular Windows Presentation Foundation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralA good starting point. but ...membertabor253:18 30 Apr '08  
GeneralRe: A good starting point. but ... [modified]memberNick Polyak3:48 30 Apr '08  
GeneralRe: A good starting point. but ...membertabor257:16 1 May '08  
GeneralRe: A good starting point. but ...memberNick Polyak16:12 1 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Mar 2008
Editor: Deeksha Shenoy
Copyright 2008 by Nick Polyak
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project