internal void HandleCollapse(TreeViewItemEx container) { Debug.Assert(container != null); // Unselect all collapsed items. bool childFocused = false; bool childSelected = false; foreach (var childContainer in GetAllContainers(container, true)) { if (childContainer.IsKeyboardFocusWithin) { childFocused = true; } if (childContainer.IsSelected) { childSelected = true; childContainer.IsSelected = false; } } if (childSelected) { MoveSelection(container); } else if (childFocused) { container.FocusWithRetry(); } }
private void OnNavigationKey(TreeViewItemEx targetContainer) { if (targetContainer == null) { return; } if (IsControlDown) { targetContainer.FocusWithRetry(); targetContainer.BringIntoView(); } else { MoveSelection(targetContainer); } }
/// <summary> /// Invoked whenever an unhandled <see cref="UIElement.GotFocus"/> event reaches this /// element in its route. /// </summary> /// <param name="e"> /// The <see cref="RoutedEventArgs"/> that contains the event data. /// </param> protected override void OnGotFocus(RoutedEventArgs e) { base.OnGotFocus(e); // Focus last focused item or first selected item. if (_lastFocusedContainer == null) { var selectedItems = SelectedItems; if (selectedItems != null && selectedItems.Count > 0) { HandleGotFocus(GetContainer(selectedItems[0])); } } if (_lastFocusedContainer != null) { _lastFocusedContainer.FocusWithRetry(); } else { GetFirstContainer(this, true)?.FocusWithRetry(); } }
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(); }
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(); }