/// <summary> /// Sets focus to the first focusable element in the details template /// </summary> private void FocusFirstFocusableElementInDetails() { if (GetTemplateChild(PartDetailsPanel) is DependencyObject details) { var focusableElement = FocusManager.FindFirstFocusableElement(details); (focusableElement as Control)?.Focus(FocusState.Programmatic); } }
/// <summary> /// Sets focus to the first focusable element in the details template /// </summary> private void FocusFirstFocusableElementInDetails() { if (GetTemplateChild(PART_DETAILS_PANE) is DependencyObject details) { DependencyObject focusableElement = FocusManager.FindFirstFocusableElement(details); (focusableElement as Control)?.Focus(FocusState.Programmatic); } }
private async Task FocusPicker() { var element = FocusManager.FindFirstFocusableElement(_rootGrid); if (element != null) { await FocusManager.TryFocusAsync(element, FocusState.Programmatic); } }
private void inputButton_KeyDown(object sender, KeyRoutedEventArgs e) { DependencyObject candidate; if (e.Key == Windows.System.VirtualKey.Down) { e.Handled = true; candidate = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Down); if (candidate != null) { (candidate as Control).Focus(FocusState.Keyboard); } else { candidate = FocusManager.FindFirstFocusableElement(candidate); if (candidate != null) { (candidate as Control).Focus(FocusState.Keyboard); } } } else if (e.Key == Windows.System.VirtualKey.Up) { e.Handled = true; candidate = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Up); if (candidate != null) { (candidate as Control).Focus(FocusState.Keyboard); } else { candidate = FocusManager.FindLastFocusableElement(candidate); if (candidate != null) { (candidate as Control).Focus(FocusState.Keyboard); } } } }
Control FindFocusCandidate(int clearedIndex, out UIElement focusedChild) { // Walk through all the children and find elements with index before and after the cleared index. // Note that during a delete the next element would now have the same index. int previousIndex = int.MinValue; int nextIndex = int.MaxValue; UIElement nextElement = null; UIElement previousElement = null; var children = m_owner.Children; for (int i = 0; i < children.Count; ++i) { var child = children[i]; var virtInfo = ItemsRepeater.TryGetVirtualizationInfo(child); if (virtInfo != null && virtInfo.IsHeldByLayout) { int currentIndex = virtInfo.Index; if (currentIndex < clearedIndex) { if (currentIndex > previousIndex) { previousIndex = currentIndex; previousElement = child; } } else if (currentIndex >= clearedIndex) { // Note that we use >= above because if we deleted the focused element, // the next element would have the same index now. if (currentIndex < nextIndex) { nextIndex = currentIndex; nextElement = child; } } } } // Find the next element if one exists, if not use the previous element. // If the container itself is not focusable, find a descendent that is. Control focusCandidate = null; focusedChild = null; if (nextElement != null) { focusedChild = nextElement as UIElement; focusCandidate = nextElement as Control; if (focusCandidate == null) { var firstFocus = FocusManager.FindFirstFocusableElement(nextElement); if (firstFocus != null) { focusCandidate = firstFocus as Control; } } } if (focusCandidate == null && previousElement != null) { focusedChild = previousElement as UIElement; focusCandidate = previousElement as Control; if (previousElement == null) { var lastFocus = FocusManager.FindLastFocusableElement(previousElement); if (lastFocus != null) { focusCandidate = lastFocus as Control; } } } return(focusCandidate); }