private void OnNavigationKey(TreeViewItemEx targetContainer) { if (targetContainer == null) { return; } if (IsControlDown) { targetContainer.FocusWithRetry(); targetContainer.BringIntoView(); } else { MoveSelection(targetContainer); } }
internal void MoveSelection(TreeViewItemEx container) { Debug.Assert(container != null); var selectedItems = SelectedItems; if (selectedItems != null) { var item = container.Item; if (selectedItems.Count == 1 && selectedItems[0] == item) { // Focused moved back to current and only selected item. _shiftSelectionAnchor = container; } else if (IsControlDown) { // Ctrl-click item toggles selection. if (selectedItems.Contains(item)) { selectedItems.Remove(item); } else { selectedItems.Add(item); } _shiftSelectionAnchor = container; } else if (IsShiftDown && selectedItems.Count > 0) { // Shift-click extends selection. var anchorContainer = _shiftSelectionAnchor ?? GetContainer(selectedItems[0]); var rangeItems = GetRangeItems(this, anchorContainer, container, true); for (int i = selectedItems.Count - 1; i >= 0; i--) { if (!rangeItems.Contains(selectedItems[i])) { selectedItems.RemoveAt(i); } } for (int i = 0; i < rangeItems.Count; i++) { if (!selectedItems.Contains(rangeItems[i])) { selectedItems.Add(rangeItems[i]); } } } else { // Single selection. for (int i = selectedItems.Count - 1; i >= 0; i--) { if (selectedItems[i] != item) { selectedItems.RemoveAt(i); } } if (!selectedItems.Contains(item)) { selectedItems.Add(item); } _shiftSelectionAnchor = container; } } container.FocusWithRetry(); container.BringIntoView(); }