Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The code below works fine and is much simpler. However the draw back is the use of the non public property, IsSelectionChangeActive, of the treeview class. Code below:</p> <pre><code>private static readonly PropertyInfo IsSelectionChangeActiveProperty = typeof (TreeView).GetProperty ( "IsSelectionChangeActive", BindingFlags.NonPublic | BindingFlags.Instance ); public static void AllowMultiSelection(TreeView treeView) { if (IsSelectionChangeActiveProperty==null) return; var selectedItems = new List&lt;TreeViewItem&gt;(); treeView.SelectedItemChanged += (a, b) =&gt; { var treeViewItem = treeView.SelectedItem as TreeViewItem; if (treeViewItem == null) return; // allow multiple selection // when control key is pressed if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { // suppress selection change notification // select all selected items // then restore selection change notifications var isSelectionChangeActive = IsSelectionChangeActiveProperty.GetValue(treeView, null); IsSelectionChangeActiveProperty.SetValue(treeView, true, null); selectedItems.ForEach(item =&gt; item.IsSelected = true); IsSelectionChangeActiveProperty.SetValue ( treeView, isSelectionChangeActive, null ); } else { // deselect all selected items except the current one selectedItems.ForEach(item =&gt; item.IsSelected = (item == treeViewItem) ); selectedItems.Clear(); } if (!selectedItems.Contains(treeViewItem)) { selectedItems.Add(treeViewItem); } else { // deselect if already selected treeViewItem.IsSelected = false; selectedItems.Remove(treeViewItem); } }; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload