Ejemplo n.º 1
0
        public static IPopupItem NavigateItem(this IPopupItem item, bool openChildren = true)
        {
            if (item == null)
            {
                return(null);
            }
            if (item is IPopupRoot)
            {
                ((IPopupRoot)item).PopupManager.Tracking = false;
                if (!((DependencyObject)item).IsInMainFocusScope())
                {
                    Keyboard.Focus(null);
                }
                return(null);
            }
            var c = item.FirstFocusableElement();

            if (c == null)
            {
                return(null);
            }

            var pm = item.PopupRoot.PopupManager;

            pm.OpenedItem = (!openChildren || item.ParentItem() != null) ? item.ParentItem() : item;
            c.Focus();
            return(item);
        }
Ejemplo n.º 2
0
        public static void OnKeyDownNavigate(this IPopupItem item, KeyEventArgs e)
        {
            if (!item.Handle(e))
            {
                return;
            }

            var key = e.Key;

            var fe = item as FrameworkElement;

            if (fe != null)
            {
                if (fe.FlowDirection == FlowDirection.RightToLeft)
                {
                    switch (key)
                    {
                    case Key.Right:
                        key = Key.Left;
                        break;

                    case Key.Left:
                        key = Key.Right;
                        break;
                    }
                }
            }

            var  pm     = item.PopupRoot.PopupManager;
            bool isRoot = item.ParentItem() == null;
            var  top    = item;

            while (top.ParentItem() != null)
            {
                top = top.ParentItem();
            }

            if (key == Key.Tab)
            {
                bool shift = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
                if (isRoot)
                {
                    key = shift ? Key.Left : Key.Right;
                }
                else
                {
                    key = shift ? Key.Up : Key.Down;
                }
            }

            switch (key)
            {
            case Key.Escape:
                if (isRoot)
                {
                    Keyboard.Focus(null);
                    e.Handled = true;
                }
                else
                {
                    e.Handled = null != item.ParentItem().NavigateItem(false);
                }
                break;

            case Key.Right:
                if (isRoot)
                {
                    e.Handled = null != item.NavigateSibling(true, true);
                }
                else
                {
                    if (item.HasItems())
                    {
                        item.OnNavigateChildren();
                        e.Handled = true;
                    }
                    else
                    {
                        pm.OpenedItem = top.NavigateSibling(true, true);
                        e.Handled     = null != pm.OpenedItem;
                    }
                }
                break;

            case Key.Left:
                if (isRoot)
                {
                    e.Handled = null != item.NavigateSibling(false, true);
                }
                else
                {
                    if (item.ParentItem() != top)
                    {
                        e.Handled = null != item.ParentItem().NavigateItem();
                    }
                    else
                    {
                        pm.OpenedItem = top.NavigateSibling(false, true);
                        e.Handled     = null != pm.OpenedItem;
                    }
                }
                break;

            case Key.Up:
                if (isRoot)
                {
                    e.Handled = item.OnNavigateChildren();
                }
                else
                {
                    e.Handled = null != item.NavigateSibling(false, true);
                }
                break;

            case Key.Down:
                if (isRoot)
                {
                    e.Handled = item.OnNavigateChildren();
                }
                else
                {
                    e.Handled = null != item.NavigateSibling(true, true);
                }
                break;

            case Key.Enter:
            case Key.Space:
                e.Handled      = true;
                item.IsPressed = true;
                break;
            }
        }