private void CommandTextBox_OnKeyUp(object sender, KeyRoutedEventArgs e)
        {
            switch (e.Key)
            {
            case VirtualKey.Down:
            case VirtualKey.Up:

                if (!CommandTextBox.IsSuggestionListOpen)
                {
                    CommandTextBox.IsSuggestionListOpen = true;
                }

                break;

            case VirtualKey.Enter:

                if (string.IsNullOrWhiteSpace(CommandTextBox.Text) && !CommandTextBox.IsSuggestionListOpen)
                {
                    CommandTextBox.IsSuggestionListOpen = true;
                }
                else
                {
                    // TODO: Try to find a better way to handle [Enter] on auto-complete field.
                    // Weird way to move the focus to the primary button...

                    if (!(FocusManager.FindLastFocusableElement(this) is Control secondaryButton) ||
                        !secondaryButton.Name.Equals("SecondaryButton"))
                    {
                        return;
                    }

                    secondaryButton.Focus(FocusState.Programmatic);

                    var options = new FindNextElementOptions
                    {
                        SearchRoot = this,
                        XYFocusNavigationStrategyOverride = XYFocusNavigationStrategyOverride.Projection
                    };

                    if (FocusManager.FindNextElement(FocusNavigationDirection.Left, options) is Control primaryButton)
                    {
                        primaryButton.Focus(FocusState.Programmatic);
                    }
                }

                break;
            }
        }
예제 #2
0
        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);
                    }
                }
            }
        }
예제 #3
0
        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);
        }