2012-10-25 38 views
0

「按Ctrl + Tab」鍵,我想用標籤鍵而不是標準按Ctrl +切換標籤選項卡更改 「TAB」 鍵,而不是在TabControl的

我的代碼是

<TabControl> 
     <TabItem Header="Section 1" Name="tabSection1"> 
      <ScrollViewer> 
       <ContentPresenter Name="cntSection1" /> 
      </ScrollViewer> 
     </TabItem> 
     <TabItem Header="Section 2" Name="tabSection2"> 
      <ScrollViewer> 
       <ContentPresenter Name="cntSection2" /> 
      </ScrollViewer> 
     </TabItem>    
</TabControl> 

    <StackPanel> 
    <Button Content="Save" Name="btnSave" /> 
     <Button Content="Cancel" Name="btnCancel" IsCancel="True" /> 
    </StackPanel> 

我的每個ContentPresenters包含UserControls,其中包含多個UI元素,例如TextboxesCheckboxes

到目前爲止,我已經嘗試過,沒有運氣。

<TabControl.InputBindings> 
      <KeyBinding Key="Tab" Modifiers="Control" Command="EditingCommands.TabForward" /> 
    </TabControl.InputBindings> 

而且

<TabControl KeyboardNavigation.TabNavigation="Continue" KeyboardNavigation.ControlTabNavigation="None"> 
+0

只是一個好奇的問題...你想如何你目前'TabItem'的行爲,當它實際上已經到來內容視圖和內容有一個像一個TextBox控件可獲得焦點。所以當我們點擊「Tab」鍵時,你會期望下一個「TabItem」被選中,還是你希望「Tab」鍵能夠從當前內容視圖中關注控件?當你要求這樣的行爲時,這個考慮應該很重要。 –

+0

當按Tab鍵時,它應該將焦點移動到當前內容視圖中的控件。當到達最後一個項目時,下一個TabItem應該可見。 –

+0

我會建議使用自定義行爲來實現這一點。 'TabControl'使用'Control + Tab鍵'在'TabItems'之間切換的功能是特定的。如果有人可以使用任何設置覆蓋它,我很懷疑。 –

回答

0

我實現了類似的東西,我想TAB鍵來更改選項卡時,選項卡中的最後一個文本框,目前的重點。我用下面附加的行爲:

<TextBox local:ChangeTabsBehavior.ChangeTabs="True"/> 

你應該能夠很容易地修改此:

using System; 
using System.Linq; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 

namespace Core.Common 
{ 
    public static class ChangeTabsBehavior 
    { 
     public static bool GetChangeTabs(DependencyObject obj) 
     { 
      return (bool)obj.GetValue(ChangeTabsBehaviorProperty); 
     } 

     public static void SetChangeTabs(DependencyObject obj, bool value) 
     { 
      obj.SetValue(ChangeTabsBehaviorProperty, value); 
     } 

     public static readonly DependencyProperty ChangeTabsBehaviorProperty = 
      DependencyProperty.RegisterAttached("ChangeTabs", 
       typeof(bool), typeof(ChangeTabsBehavior), 
       new PropertyMetadata(false, OnChangeTabsChanged)); 

     private static void OnChangeTabsChanged(object sender, DependencyPropertyChangedEventArgs e) 
     { 
      var textBox = (FrameworkElement) sender; 

      var changeTabs = (bool) (e.NewValue); 

      if (changeTabs) 
       textBox.PreviewKeyDown += TextBoxOnPreviewKeyDown; 
      else 
       textBox.PreviewKeyDown -= TextBoxOnPreviewKeyDown; 
     } 

     private static void TextBoxOnPreviewKeyDown(object sender, KeyEventArgs keyEventArgs) 
     { 
      if (keyEventArgs.Key == Key.Tab) 
      { 
       var textBox = (FrameworkElement) sender; 
       var tabControl = textBox.TryFindParent<TabControl>(); 

       if (tabControl.SelectedIndex == tabControl.Items.Count - 1) 
        tabControl.SelectedIndex = 0; 
       else 
        tabControl.SelectedIndex++; 

       keyEventArgs.Handled = true; 
      } 
     } 
    } 
} 

的TryFindParent方法

你再附上的行爲在XAML像這樣見http://www.hardcodet.net/2008/02/find-wpf-parent滿足您的需求。

更多的行爲在這裏:http://www.jayway.com/2013/03/20/behaviors-in-wpf-introduction/