private void ManageShiftSelection(MultiSelectTreeViewItem viewItem) { if (_lastClickedItem == null) { _lastClickedItem = Items[0] as MultiSelectTreeViewItem; } if (_lastClickedItem == null) { return; } if (_lastClickedItem.ParentMultiSelectTreeView == null) { return; // seems to be a disconnected item } MultiSelectTreeViewItem.SelectAllNodesInbetween(_lastClickedItem, viewItem, true); }
/// <summary> /// Retrieve the subsequent node that is at the same level. /// </summary> /// <param name="item">The node starting with you want to retrieve the subsequent one.</param> /// <returns>Null if there is no subsequent node at the same level.</returns> public static MultiSelectTreeViewItem GetNextNodeAtSameLevel(MultiSelectTreeViewItem item) { if (item == null) { return(null); } MultiSelectTreeViewItem nextNodeAtSameLevel = null; ItemsControl parentControl = item.ParentItemsControl; if (parentControl != null) { int index = parentControl.ItemContainerGenerator.IndexFromContainer(item); if (index != parentControl.Items.Count - 1) // if this is not the last item { nextNodeAtSameLevel = parentControl.ItemContainerGenerator.ContainerFromIndex(index + 1) as MultiSelectTreeViewItem; } } return(nextNodeAtSameLevel); }
private void ManageCtrlSelection(MultiSelectTreeViewItem viewItem) { viewItem.IsSelected = !viewItem.IsSelected; _lastClickedItem = viewItem; }
private void ManageSingleSelection(MultiSelectTreeViewItem viewItem) { UnselectAll(); viewItem.IsSelected = true; _lastClickedItem = viewItem; }
internal void OnViewItemMouseDoubleClick(MultiSelectTreeViewItem viewItem, MouseButtonEventArgs e) { OnItemDoubleClicked(viewItem); e.Handled = true; }
/// <summary> /// Select all nodes between the two nodes given in the arguments. The two nodes might be given in arbitrary order and at arbitrary levels. /// </summary> /// <param name="item1">One node.</param> /// <param name="item2">The other node.</param> /// <param name="includingIndirectChildNodes"></param> public static void SelectAllNodesInbetween(MultiSelectTreeViewItem item1, MultiSelectTreeViewItem item2, bool includingIndirectChildNodes) { int level1 = item1.GetDepth(); int level2 = item2.GetDepth(); // swap item1 and item2, so that item2 is the item with the higher level if (level1 > level2) { var hi = item1; var hl = level1; item1 = item2; level1 = level2; item2 = hi; level2 = hl; } // get the parent node of item2, that is on the same level than item1 var item2OnLevel1 = item2; // for (int l = level2 - 1; l >= level1; --l) { item2OnLevel1 = (MultiSelectTreeViewItem)item2OnLevel1.ParentItemsControl; } // to determine which node is the first node and which the last node, find // the common anchestor of both nodes var it1 = item1; var it2 = item2OnLevel1; while (it1.ParentItemsControl != it2.ParentItemsControl) { it1 = it1.ParentItemsControl as MultiSelectTreeViewItem; it2 = it2.ParentItemsControl as MultiSelectTreeViewItem; } // now we can determine which node is first and which last // if both are identical, then item1 is the first item, since // parent nodes comes first compared to child nodes var commonParent = it1.ParentItemsControl; int idx1 = commonParent.ItemContainerGenerator.IndexFromContainer(it1); int idx2 = commonParent.ItemContainerGenerator.IndexFromContainer(it2); bool item1IsFirst = idx1 <= idx2; // Swap so that item1 gets the first item if (!item1IsFirst) { var hi = item1; var hl = level1; var hh = it1; var hx = idx1; item1 = item2; level1 = level2; it1 = it2; idx1 = idx2; item2 = hi; level2 = hl; it2 = hh; idx2 = hx; } // if item1 and item2 share the same anchestor, select from item1 to items if (item1.ParentItemsControl == commonParent && item2.ParentItemsControl == commonParent) { SelectAllChildNodesBetweenIndices(commonParent, idx1, idx2); } else { // first go down to the common anchestor, and then up var current = item1; current.IsSelected = true; var parent = current.ParentItemsControl; while (parent != commonParent) { int idx = parent.ItemContainerGenerator.IndexFromContainer(current); SelectAllChildNodesBetweenIndexAndEnd(parent, idx + 1); current = parent as MultiSelectTreeViewItem; parent = current.ParentItemsControl; } // now select all items between idx1+1 and idx2-1 (including all childs) SelectAllChildNodesBetweenIndices(commonParent, idx1 + 1, idx2 - 1); // last select from item2 downwards to the common anchestor current = item2; parent = current.ParentItemsControl; while (parent != commonParent) { int idx = parent.ItemContainerGenerator.IndexFromContainer(current); SelectAllChildNodesBetweenStartAndIndex(parent, idx); current = parent as MultiSelectTreeViewItem; parent = current.ParentItemsControl; } current.IsSelected = true; } }
protected override void OnKeyDown(KeyEventArgs e) { try { MultiSelectTreeViewItem itemToSelect = null; if (e.Key == Key.Left) { IsExpanded = false; e.Handled = true; } else if (e.Key == Key.Right) { IsExpanded = true; e.Handled = true; } else if (e.Key == Key.Up) { // In this case we need to select the last child of the last expandend node of // - the previous at the same level (if this index node is NOT 0) // - the parent node (if this index node is 0) int currentNodeIndex = ParentItemsControl.ItemContainerGenerator.IndexFromContainer(this); if (currentNodeIndex == 0) { itemToSelect = ParentMultiSelectTreeViewItem; } else { MultiSelectTreeViewItem tmp = null; tmp = GetPreviousNodeAtSameLevel(this); itemToSelect = GetLastVisibleChildNodeOf(tmp); } e.Handled = true; } else if (e.Key == Key.Down) { // In this case we need to select: // - the first child node (if this node is expanded) // - the next at the same level (if this not the last child) // - the next at the same level of the parent node (if this is the last child) if (IsExpanded && Items.Count > 0) { // Select first Child itemToSelect = ItemContainerGenerator.ContainerFromIndex(0) as MultiSelectTreeViewItem; } else { itemToSelect = GetNextNodeAtSameLevel(this); if (itemToSelect == null) // current node has no subsequent node at the same level { MultiSelectTreeViewItem tmp = ParentMultiSelectTreeViewItem; while (itemToSelect == null && tmp != null) // searhing for the first parent that has a subsequent node at the same level { itemToSelect = GetNextNodeAtSameLevel(tmp); tmp = tmp.ParentMultiSelectTreeViewItem; } } } e.Handled = true; } if (itemToSelect != null) { itemToSelect.Focus(); itemToSelect.IsSelected = true; ParentMultiSelectTreeView.OnItemClicked(itemToSelect); } } catch (Exception) { /* Silently ignore */ } }