2010-07-18 6 views
0

J'ai créé un contrôle TreeViewComboBox, dans ce cas, j'ai une zone de liste déroulante avec des éléments comme TreeView, mais la navigation clavier ne fonctionne pas sur TreeView. Je ne suis pas capable de naviguer à travers les TreeViewItems en utilisant le clavier. Toute aide plz?La navigation au clavier ne fonctionne pas

Répondre

0

J'ai un problème similaire lors de la mise en œuvre de notre contrôle de grille de propriété. Le problème dans notre spécifique, qui pourrait être semblable au vôtre, était que les événements de focus de clavier n'étaient pas le routage au TreeViewItem.

La façon dont nous l'avons résolu était d'ajouter un objet AttachedProperty au style TreeViewItem et de sélectionner le noeud lorsqu'il obtient le focus du clavier. J'ai implémenté ceci en utilisant un modèle Service. Voir ci-dessous:

En XAML:

<Style x:Key="EditorBorder" TargetType="{x:Type Border}"> 
    <Setter Property="Controls_Services:TreeViewItemService.SelectWhenKeyboardIsFocused" Value="True"/> 
</Style> 

En Codebehind:

public class TreeViewItemService 
    { 
    public static readonly DependencyProperty SelectWhenKeyboardIsFocusedProperty = DependencyProperty.RegisterAttached("SelectWhenKeyboardIsFocused", 
     typeof(bool), typeof(TreeViewItemService), new FrameworkPropertyMetadata(false, TreeViewItemService.OnSelectWhenKeyboardIsFocusedPropertyChanged)); 

    static void OnSelectWhenKeyboardIsFocusedPropertyChanged(object sender, DependencyPropertyChangedEventArgs e) 
    { 
     FrameworkElement element = sender as FrameworkElement; 
     if (element == null) 
     return; 

     TreeViewItem target = MTVisualTreeHelper.FindParent<TreeViewItem>(element as DependencyObject); 
     if (target != null) 
     new IsSelectedOnKeyboardFocusAction(element, target); 
    } 

    public static bool GetSelectWhenKeyboardIsFocused(FrameworkElement target) 
    { 
     return (bool)target.GetValue(SelectWhenKeyboardIsFocusedProperty); 
    } 

    public static void SetSelectWhenKeyboardIsFocused(FrameworkElement target, bool value) 
    { 
     target.SetValue(SelectWhenKeyboardIsFocusedProperty, value); 
    } 

    private class IsSelectedOnKeyboardFocusAction 
    { 
     TreeViewItem m_Target; 
     FrameworkElement m_Source; 

     public IsSelectedOnKeyboardFocusAction(FrameworkElement source, TreeViewItem item) 
     { 
     m_Source = source; 
     m_Target = item; 
     m_Source.Loaded += OnSource_Loaded; 
     m_Source.Unloaded += OnSource_Unloaded; 
     } 

     void OnSource_Loaded(object sender, RoutedEventArgs e) 
     { 
     m_Source.PreviewMouseLeftButtonDown += OnSource_PreviewMouseLeftButtonDown; 
     m_Source.GotFocus += OnSource_GotFocus; 
     m_Source.LostFocus += OnSource_LostFocus; 
     m_Source.GotKeyboardFocus += OnSource_GotKeyboardFocus; 
     m_Source.LostKeyboardFocus += OnSource_LostKeyboardFocus; 
     } 

     void OnSource_Unloaded(object sender, RoutedEventArgs e) 
     { 
     m_Source.PreviewMouseLeftButtonDown -= OnSource_PreviewMouseLeftButtonDown; 
     m_Source.GotFocus -= OnSource_GotFocus; 
     m_Source.LostFocus -= OnSource_LostFocus; 
     m_Source.GotKeyboardFocus -= OnSource_GotKeyboardFocus; 
     m_Source.LostKeyboardFocus -= OnSource_LostKeyboardFocus; 
     } 

     void OnSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 
     if (!m_Target.IsSelected) 
      m_Target.IsSelected = true; 
     } 

     void OnSource_GotFocus(object sender, RoutedEventArgs e) 
     { 
     if (!m_Target.IsSelected) 
      m_Target.IsSelected = true; 
     } 

     void OnSource_LostFocus(object sender, RoutedEventArgs e) 
     { 
     if (m_Target.IsSelected) 
      m_Target.IsSelected = false; 
     } 

     void OnSource_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) 
     { 
     if (!m_Target.IsSelected) 
      m_Target.IsSelected = true; 
     } 

     void OnSource_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) 
     { 
     if (m_Target.IsSelected) 
      m_Target.IsSelected = false; 
     } 

    } 

    } 

HTH,

Questions connexes