Exemplo n.º 1
0
 /// <summary>
 /// Adds a tree view item to the multi-selection without going through
 /// the normal event chain. Used only by TreeViewItemMultiSelect.
 /// </summary>
 /// <param name="newItem"></param>
 internal void AddTreeViewItemToSelection(TreeViewItemMultiSelect newItem)
 {
     if (!selectedTreeViewItems.Contains(newItem))
     {
         selectedTreeViewItems.Add(newItem);
     }
     previousItem = newItem;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Removes the item passed in to from the selected items
 /// </summary>
 /// <param name="userItemToDeselect"></param>
 public void DeselectItem(object itemToDeselect)
 {
     if (itemToTreeViewItem.ContainsKey(itemToDeselect))
     {
         TreeViewItemMultiSelect treeViewItem = itemToTreeViewItem[itemToDeselect];
         treeViewItem.IsSelected = false;
         if (selectedTreeViewItems.Contains(treeViewItem))
         {
             selectedTreeViewItems.Remove(treeViewItem);
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Handles when the selected item changes. Handles the multi-select functionality.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSelectedItemChanged(RoutedPropertyChangedEventArgs <object> e)
        {
            TreeViewItemMultiSelect newItem = (TreeViewItemMultiSelect)typeof(TreeView).GetField("_selectedContainer", BindingFlags.NonPublic | BindingFlags.Instance).GetValue((TreeView)this);

            // Turn off updating from TreeViewItems
            isSelectionChangeActiveProperty.SetValue((TreeView)this, true, null);
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                // We are either :
                // a ) Adding a new item so newItem!=null & !selectedTreeViewItems.Contains(newItem)
                // b ) Removing the last selection so newItem==null;
                // c ) Removing a previous selection selectedTreeViewItems.Contains(newItem)


                if (newItem != null && !selectedTreeViewItems.Contains(newItem))
                {
                    // Adding a new item
                    if (previousItem != null)
                    {
                        previousItem.IsSelected = true;
                    }
                    selectedTreeViewItems.Add(newItem);
                    previousItem = newItem;
                }
                else if (newItem == null && previousItem != null)
                {
                    // Removing the last selected item

                    selectedTreeViewItems.Remove(previousItem);
                    previousItem = null;
                }
                else if (selectedTreeViewItems.Contains(newItem))
                {
                    // Removing a previously selected item

                    newItem.IsSelected = false;
                    selectedTreeViewItems.Remove(newItem);

                    // The selection needs to be put back on the last selected item
                    if (previousItem != null)
                    {
                        previousItem.IsSelected = true;
                    }
                    previousItem = null;
                }
            }
            else
            {
                while (selectedTreeViewItems.Count > 0)
                {
                    TreeViewItemMultiSelect firstItem = selectedTreeViewItems[0];
                    if (firstItem.IsSelected && firstItem != newItem)
                    {
                        firstItem.IsSelected = false;
                    }
                    selectedTreeViewItems.RemoveAt(0);
                }
                if (newItem != null)
                {
                    selectedTreeViewItems.Add(newItem);
                }

                previousItem = newItem;
            }
            // Turn back on updating from TreeViewItems
            isSelectionChangeActiveProperty.SetValue((TreeView)this, false, null);

            // when the newItem is null that's when the last item was deselected.
            base.OnSelectedItemChanged(e);
        }