0

I have a WPF-C# application with a ribbon menu and several text box into the main panel. when i focus one of them and press the left arrow, i get the following error. when i press on any other arrow it works well. i tried to breakpoint on a previewKeyDoyn of a specific textbox method but the Exception is thrown before i can ran into it.

All other textbox in my application work well when i press the left arrow.

System.ComponentModel.InvalidEnumArgumentException: La valeur de l'argument 'direction' (3) n'est pas valide pour le type Enum 'FocusNavigationDirection'.
Nom du paramètre : direction
   à System.Windows.Input.KeyboardNavigation.IsInDirection(Rect fromRect, Rect toRect, FocusNavigationDirection direction)
   à System.Windows.Input.KeyboardNavigation.FindNextInDirection(DependencyObject sourceElement, Rect sourceRect, DependencyObject container, FocusNavigationDirection direction, Double startRange, Double endRange)
   à System.Windows.Input.KeyboardNavigation.MoveNext(DependencyObject sourceElement, DependencyObject container, FocusNavigationDirection direction, Double startRange, Double endRange)
   à System.Windows.Input.KeyboardNavigation.GetNextInDirection(DependencyObject sourceElement, FocusNavigationDirection direction)
   à System.Windows.Input.KeyboardNavigation.PredictFocusedElement(DependencyObject sourceElement, FocusNavigationDirection direction)
   à System.Windows.FrameworkElement.PredictFocus(FocusNavigationDirection direction)
   à Microsoft.Windows.Controls.Ribbon.RibbonHelper.PredictFocus(DependencyObject element, FocusNavigationDirection direction) dans e:\dd\WPFOOB\src\wpfoob\Ribbon\RibbonControlsLibrary\Microsoft\Windows\Controls\Ribbon\RibbonHelper.cs:ligne 488
   à Microsoft.Windows.Controls.Ribbon.RibbonApplicationMenu.OnPreviewKeyDown(KeyEventArgs e) dans e:\dd\WPFOOB\src\wpfoob\Ribbon\RibbonControlsLibrary\Microsoft\Windows\Controls\Ribbon\RibbonApplicationMenu.cs:ligne 520
   à System.Windows.UIElement.OnPreviewKeyDownThunk(Object sender, KeyEventArgs e)
   à System.Windows.Input.KeyEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   à System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   à System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   à System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   à System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   à System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   à System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   à System.Windows.Input.InputManager.ProcessStagingArea()
   à System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   à System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   à System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey)
   à System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(MSG& msg, Boolean& handled)
   à System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG& msg, ModifierKeys modifiers)
   à System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
   à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   à MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
4

1 回答 1

0

I decompiled the ribbonControlsLibrary.dll assembly with .NET reflector and i found this part of code. if we look carefully, we can see that right, up, down and last are used. it should be left instead of last. the only way to resolve the problem is to use .NET framework 4.5 or to override OnPreviewKeyDown to handle the left key (so the exception is not thrown but the caret don't move to previous character).

my code:

protected override void OnPreviewKeyDown(KeyEventArgs e)
{
    if (e.Key == Key.Left)
    {
        e.Handled = true;
    }
}

RibbonControlsLibrary.dll:

protected override void OnPreviewKeyDown(KeyEventArgs e)
{
    if (!e.Handled)
    {
        DependencyObject originalSource;
        DependencyObject obj3;
        if (e.Key == Key.Down)
        {
            originalSource = e.OriginalSource as DependencyObject;
            if (originalSource != null)
            {
                UIElement footerPaneHost = this.FooterPaneHost;
                if (((footerPaneHost != null) && footerPaneHost.IsKeyboardFocusWithin) && TreeHelper.IsVisualAncestorOf(footerPaneHost, originalSource))
                {
                    obj3 = RibbonHelper.PredictFocus(originalSource, FocusNavigationDirection.Down);
                    if (((obj3 == null) || (obj3 == originalSource)) && (this.ItemsPaneMoveFocus(FocusNavigationDirection.First) || this.AuxiliaryPaneMoveFocus(FocusNavigationDirection.First)))
                    {
                        e.Handled = true;
                    }
                }
            }
        }
        else
        {
            UIElement auxiliaryPaneHost;
            if (e.Key == Key.Up)
            {
                UIElement element2 = this._popup.TryGetChild();
                if ((element2 != null) && !element2.IsKeyboardFocusWithin)
                {
                    if (this.FooterPaneMoveFocus(FocusNavigationDirection.Last) || this.AuxiliaryPaneMoveFocus(FocusNavigationDirection.Last))
                    {
                        e.Handled = true;
                    }
                }
                else
                {
                    originalSource = e.OriginalSource as DependencyObject;
                    if (originalSource != null)
                    {
                        auxiliaryPaneHost = this.AuxiliaryPaneHost;
                        if (((auxiliaryPaneHost != null) && auxiliaryPaneHost.IsKeyboardFocusWithin) && TreeHelper.IsVisualAncestorOf(auxiliaryPaneHost, originalSource))
                        {
                            obj3 = RibbonHelper.PredictFocus(originalSource, FocusNavigationDirection.Up);
                            if (((obj3 == null) || (obj3 == originalSource)) && (this.ItemsPaneMoveFocus(FocusNavigationDirection.Last) || this.FooterPaneMoveFocus(FocusNavigationDirection.Last)))
                            {
                                e.Handled = true;
                            }
                        }
                    }
                }
            }
            else if ((e.Key == Key.Left) || (e.Key == Key.Right))
            {
                originalSource = e.OriginalSource as DependencyObject;
                if (originalSource != null)
                {
                    if ((e.Key == Key.Left) == (base.FlowDirection == FlowDirection.LeftToRight))
                    {
                        auxiliaryPaneHost = this.AuxiliaryPaneHost;
                        if (((auxiliaryPaneHost != null) && auxiliaryPaneHost.IsKeyboardFocusWithin) && TreeHelper.IsVisualAncestorOf(auxiliaryPaneHost, originalSource))
                        {
                            obj3 = RibbonHelper.PredictFocus(originalSource, FocusNavigationDirection.Last);
                            if (((obj3 != null) && !TreeHelper.IsVisualAncestorOf(auxiliaryPaneHost, obj3)) && RibbonHelper.Focus(obj3))
                            {
                                e.Handled = true;
                            }
                        }
                    }
                    else if (e.Key == Key.Left)
                    {
                        ScrollViewer subMenuScrollViewer = base.SubMenuScrollViewer;
                        if (((subMenuScrollViewer != null) && subMenuScrollViewer.IsKeyboardFocusWithin) && TreeHelper.IsVisualAncestorOf(subMenuScrollViewer, originalSource))
                        {
                            RibbonMenuItem item = originalSource as RibbonMenuItem;
                            if (item == null)
                            {
                                item = TreeHelper.FindVisualAncestor<RibbonMenuItem>(originalSource);
                            }
                            if ((item != null) && !item.CanOpenSubMenu)
                            {
                                obj3 = item.PredictFocus(FocusNavigationDirection.Right);
                                if ((obj3 != null) && RibbonHelper.Focus(obj3))
                                {
                                    e.Handled = true;
                                }
                            }
                        }
                    }
                }
            }
        }
        base.OnPreviewKeyDown(e);
    }
}

FocusNavigationDirection:

public enum FocusNavigationDirection
{
    // Résumé :
    //     Déplacer le focus sur l'élément pouvant être actif suivant dans l'ordre de
    //     tabulation.Non pris en charge pour System.Windows.UIElement.PredictFocus(System.Windows.Input.FocusNavigationDirection).
    Next = 0,
    //
    // Résumé :
    //     Déplacer le focus sur l'élément pouvant être actif précédent dans l'ordre
    //     de tabulation.Non pris en charge pour System.Windows.UIElement.PredictFocus(System.Windows.Input.FocusNavigationDirection).
    Previous = 1,
    //
    // Résumé :
    //     Déplacer le focus sur le premier élément pouvant être actif dans l'ordre
    //     de tabulation.Non pris en charge pour System.Windows.UIElement.PredictFocus(System.Windows.Input.FocusNavigationDirection).
    First = 2,
    //
    // Résumé :
    //     Déplacer le focus sur le dernier élément pouvant être actif dans l'ordre
    //     de tabulation.Non pris en charge pour System.Windows.UIElement.PredictFocus(System.Windows.Input.FocusNavigationDirection).
    Last = 3,
    //
    // Résumé :
    //     Déplacer le focus sur un autre élément pouvant être actif et situé à gauche
    //     de l'élément ayant actuellement le focus.
    Left = 4,
    //
    // Résumé :
    //     Déplacer le focus sur un autre élément pouvant être actif et situé à droite
    //     de l'élément ayant actuellement le focus.
    Right = 5,
    //
    // Résumé :
    //     Déplacer le focus sur un autre élément pouvant être actif et situé plus haut
    //     par rapport à l'élément ayant actuellement le focus.
    Up = 6,
    //
    // Résumé :
    //     Déplacer le focus sur un autre élément pouvant être actif et situé plus bas
    //     par rapport à l'élément ayant actuellement le focus.
    Down = 7,
}
于 2013-07-26T12:16:45.340 回答