键盘导航无法正常工作

时间:2010-07-18 05:01:03

标签: c# wpf combobox treeview keyboard-navigation

我已经创建了一个TreeViewComboBox控件,在这里我有一个ComboBox,其项目为TreeView,但是键盘导航在TreeView上不起作用。我无法使用键盘浏览TreeViewItems。任何帮助PLZ?

1 个答案:

答案 0 :(得分:0)

我在实现属性网格控件时遇到了类似的问题。 我们的特定的问题,可能与您的相似,是键盘焦点事件未路由到TreeViewItem

我们解决它的方法是在TreeViewItem样式中添加AttachedProperty,并在获得键盘焦点时选择节点。我使用 Service 模式实现了这一点。见下文:

在XAML中:

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

在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,