2010-09-13 43 views
1

如何通過按下向下箭頭將焦點從tabitem標題移動到此tabitem的內容? 我嘗試使用KeyboardNavigation,但當按下或向上鍵時Keyboardfocus仍然不動。如何將焦點從tabitem標題移至WPF中的內容?

在此先感謝。

+0

哦,好。我不知道這件事。我瀏覽了我的問題並指出了一些答案。 – Void 2010-09-14 06:48:38

回答

1

我創建了一個附屬屬性來解決類似的問題。當您按ContentControl(TabItem)上的某個鍵時,它將重點放在內容上。在XAML看起來像這樣

<TabControl Focusable="False"> 
     <TabItem Header="Main" local:Focus.ContentOn="Down"> 
      <Stackpanel> 
       <TextBox /> 
      </Stackpanel> 
     </TabItem> 

而且附加屬性:

public static class Focus 
{ 
    public static Key GetContentOn(DependencyObject obj) 
    { 
     return (Key)obj.GetValue(ContentOnProperty); 
    } 

    public static void SetContentOn(DependencyObject obj, Key value) 
    { 
     obj.SetValue(ContentOnProperty, value); 
    } 

    // Using a DependencyProperty as the backing store for ContentOn. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ContentOnProperty = 
     DependencyProperty.RegisterAttached("ContentOn", typeof(Key), typeof(Navigate), 
     new FrameworkPropertyMetadata(Key.None, OnContentOnChanged)); 

    private static void OnContentOnChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) 
    { 
     var control = o as ContentControl; 
     if (control != null) 
      control.KeyDown += FocusContent; 
    } 

    private static void FocusContent(object sender, KeyEventArgs e) 
    { 
     if (Keyboard.FocusedElement == sender) 
     { 
      var control = sender as ContentControl; 
      if (control != null && control.HasContent && GetContentOn(control) == e.Key) 
      { 
       ((FrameworkElement)control.Content).MoveFocus(new TraversalRequest(FocusNavigationDirection.First)); 
       e.Handled = true; 
      } 
     } 
    } 
}