Exemplo n.º 1
0
        /// <summary>
        /// Attempts to navigate focus in the specified direction.
        /// </summary>
        /// <param name="view">The view for which to perform navigation.</param>
        /// <param name="element">The element at which to begin navigation.</param>
        /// <param name="direction">The direction in which to navigate focus.</param>
        /// <param name="ctrl">A value indicating whether the Ctrl modifier is active.</param>
        /// <returns><c>true</c> if navigation was performed; otherwise, <c>false</c>.</returns>
        public static Boolean PerformNavigation(PresentationFoundationView view, UIElement element, FocusNavigationDirection direction, Boolean ctrl)
        {
            if (!PrepareNavigation(view, ref element, ref direction))
                return false;

            var navprop      = GetNavigationProperty(direction, ctrl);
            var navContainer = default(DependencyObject);
            var destination  = default(IInputElement);
            
            switch (direction)
            {
                case FocusNavigationDirection.Next:
                    navContainer = FindNavigationContainer(element, navprop);
                    destination  = FindNextNavigationStop(view, navContainer, element, navprop, false) as IInputElement;
                    break;

                case FocusNavigationDirection.Previous:
                    navContainer = FindNavigationContainer(element, navprop);
                    destination  = FindPrevNavigationStop(view, navContainer, element, navprop, false) as IInputElement;
                    break;

                case FocusNavigationDirection.First:
                    destination = FindNextNavigationStop(view, element, null, navprop, true) as IInputElement;
                    break;

                case FocusNavigationDirection.Last:
                    destination = FindPrevNavigationStop(view, element, null, navprop, true) as IInputElement;
                    break;

                case FocusNavigationDirection.Left:
                case FocusNavigationDirection.Right:
                case FocusNavigationDirection.Up:
                case FocusNavigationDirection.Down:
                    navContainer = FindNavigationContainer(element, navprop, false);
                    if (navContainer != null)
                    {
                        destination = FindNavigationStopInDirection(view, navContainer, element, navprop, direction) as IInputElement;
                    }
                    break;
            }            

            if (destination != null)
            {
                view.FocusElement(destination);
                return true;
            }
            return false;
        }