/// <summary>
        /// Performs tab navigation while focus is within the drop down.
        /// </summary>
        private void PerformTabNavigation(Key key, ModifierKeys modifiers)
        {
            var shift = ((modifiers & ModifierKeys.Shift) == ModifierKeys.Shift);
            var ctrl  = ((modifiers & ModifierKeys.Control) == ModifierKeys.Control);

            var focused = Keyboard.GetFocusedElement(View) as UIElement;

            if (FocusNavigator.PerformNavigation(View, focused, shift ? FocusNavigationDirection.Previous : FocusNavigationDirection.Next, ctrl))
            {
                focused = Keyboard.GetFocusedElement(View) as UIElement;

                var container = (focused == null) ? null : ItemsControlUtil.FindContainer <ComboBoxItem>(this, focused);
                if (container != null)
                {
                    ItemsControlUtil.ScrollItemIntoView <ComboBoxItem>(this, PART_ScrollViewer, container);
                }
            }
        }
        /// <summary>
        /// Moves keyboard focus the specified number of steps away from the currently focused item.
        /// </summary>
        /// <param name="step">The number of steps by which to move focus.</param>
        private void MoveItemFocus(Int32 step)
        {
            var selectedContainer = ItemsControlUtil.FindFocusedContainer <ComboBoxItem>(this);
            var selectedIndex     = (selectedContainer == null) ? -1 : ItemContainers.IndexOf(selectedContainer);

            var targetIndex = selectedIndex + step;

            if (targetIndex < 0 || targetIndex >= Items.Count)
            {
                return;
            }

            var targetContainer = ItemContainers[targetIndex] as UIElement;

            if (targetContainer == null)
            {
                return;
            }

            targetContainer.Focus();

            ItemsControlUtil.ScrollItemIntoView <ComboBoxItem>(this, PART_ScrollViewer, targetContainer);
        }
Beispiel #3
0
 /// <summary>
 /// Selects the specified item and scrolls it into view.
 /// </summary>
 private void HandleItemClickedAndScrollIntoView(ListBoxItem item, Boolean buffer = true)
 {
     HandleItemClicked(item);
     ItemsControlUtil.ScrollItemIntoView <ListBoxItem>(this, PART_ScrollViewer, item, buffer);
 }
        /// <summary>
        /// Handles <see cref="Keyboard.KeyDownEvent"/> when the drop down is open.
        /// </summary>
        private void OnKeyDown_DropDownOpen(KeyboardDevice device, Key key, ModifierKeys modifiers, RoutedEventData data)
        {
            switch (key)
            {
            case Key.Tab:
                PerformTabNavigation(key, modifiers);
                data.Handled = true;
                break;

            case Key.Escape:
                IsDropDownOpen = false;
                data.Handled   = true;
                break;

            case Key.Return:
                SelectFocusedItem();
                IsDropDownOpen = false;
                data.Handled   = true;
                break;

            case Key.Up:
                MoveItemFocus(-1);
                data.Handled = true;
                break;

            case Key.Down:
                MoveItemFocus(1);
                data.Handled = true;
                break;

            case Key.PageUp:
                var pageUpTarget = ItemsControlUtil.GetPageUpNext <ComboBoxItem>(this, PART_ScrollViewer);
                if (pageUpTarget != null && pageUpTarget.Focus())
                {
                    ItemsControlUtil.ScrollItemIntoView <ComboBoxItem>(this, PART_ScrollViewer, pageUpTarget, false);
                }
                data.Handled = true;
                break;

            case Key.PageDown:
                var pageDownTarget = ItemsControlUtil.GetPageDownNext <ComboBoxItem>(this, PART_ScrollViewer);
                if (pageDownTarget != null && pageDownTarget.Focus())
                {
                    ItemsControlUtil.ScrollItemIntoView <ComboBoxItem>(this, PART_ScrollViewer, pageDownTarget, false);
                }
                data.Handled = true;
                break;

            case Key.Home:
                var firstItem = ItemsControlUtil.GetFirstItem <ComboBoxItem>(this, PART_ScrollViewer);
                if (firstItem != null && firstItem.Focus())
                {
                    ItemsControlUtil.ScrollItemIntoView <ComboBoxItem>(this, PART_ScrollViewer, firstItem);
                }
                data.Handled = true;
                break;

            case Key.End:
                var lastItem = ItemsControlUtil.GetLastItem <ComboBoxItem>(this, PART_ScrollViewer);
                if (lastItem != null && lastItem.Focus())
                {
                    ItemsControlUtil.ScrollItemIntoView <ComboBoxItem>(this, PART_ScrollViewer, lastItem);
                }
                data.Handled = true;
                break;

            case Key.Left:
                PART_ScrollViewer?.LineLeft();
                data.Handled = true;
                break;

            case Key.Right:
                PART_ScrollViewer?.LineRight();
                data.Handled = true;
                break;
            }
        }