Calender

S M T W T F S
 123456
78910111213
14151617181920
21222324252627
28293031   
<< October 2018 >>

Categories

Archives

Recent Entries

Recent Comment

Recent Trackback

w closet×JUGEM

-

WPF MultiSelect TreeView Sample - CodeProject

@media print { body { margin: 2mm 9mm; } .original-url { display: none; } #article .float.left { float: left !important; } #article .float.right { float: right !important; } #article .float { margin-top: 0 !important; margin-bottom: 0 !important; } } WPF MultiSelect TreeView Sample - CodeProject

WPF MultiSelect TreeView Sample

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:

Hide   Copy Code

treeViewItem.IsSelected = false;

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

Hide   Copy Code

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:

Hide   Copy Code

ChangeSelectedState(treeViewItem);

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

Hide   Copy Code

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

History

  • 23rd March, 2008: Initial post

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



iPadから送信
  • 2018.08.07 Tuesday
  • 10:27

Comment
Send Comment








   
この記事のトラックバックURL
Trackback