我發現KeyboardNavigation無法正常工作,因爲它屬於Ctrl-Tab和TabControl。我把一個簡單的原型和KeyboardNavigation.ControlTabNavigation="None"
放在一起似乎沒有預期的影響,使用Ctrl-Tab切換標籤,一旦我左鍵單擊一個標籤並且鍵盤焦點在TabControl內。
但是,使用InputBindings和Command可以覆蓋不需要的Ctrl-Tab默認行爲。從那裏,我發現KeyboardNavigation.TabNavigation="Cycle"
以及TabNavigation的其他選項似乎表現合理。使用下面資源鏈接中介紹的FocusManager和其他技術應該允許獲得所需的鍵盤導航,儘管使用了一些反直覺技術。
對於具有不需要的默認行爲的每個控件,必須設置InputBindings,但是更復雜的解決方案可能會使可視樹遍歷某個類型的所有控件的InputBindings。我發現讓命令完全不會中和關鍵序列。在我的例子中,我顯示一個測試對話框。
請注意,下面的命令綁定要求您定位WPF 4.0;請參閱資源在後的資源上如何定位WPF 3.5的結束或更早
在XAML:
<TabControl
x:Name="tabControl1"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedTabItem}"
ItemsSource="{Binding TabItemViewModels}"
KeyboardNavigation.ControlTabNavigation="None"
KeyboardNavigation.TabNavigation="Continue">
<TabControl.InputBindings>
<KeyBinding Modifiers="Control"
Key="Tab"
Command="{Binding ShowDialogCommand}" />
</TabControl.InputBindings>
</TabControl>
注意,在上面的XAML,KeyboardNavigation.ControlTabNavigation="None"
是沒有效果,可以消除。
在背襯的DataContext,典型地一個ViewModel:
聲明你的結合特性:
public RelayCommand ShowDialogCommand
{
get;
private set;
}
初始化屬性;例如,可以在視圖模型的構造(注意,RelayCommand是從MVVM-光庫。):
ShowDialogCommand = new RelayCommand(() =>
{
MessageBox.Show("Show dialog box command executed", "Show Dialog Box Command", MessageBoxButton.OK, MessageBoxImage.Information);
});
資源:
Helpful StackOverflow post on KeyBindings
More detail on KeyBinding to a Command; describes special CommandReference technique needed if targeting WPF framewrok 3.5 or earlier
Microsoft's Focus Overview
我在某種程度上遇到了同樣的情況。我能夠在TabControl上使用KeyboardNavigation.ControlTabNavigation =「None」,但是,**直到** I鼠標選擇(即,左鍵單擊)Tab。在那個時候,我可以再次在選項卡上按Ctrl-Tab,這與我尋求(和期望)發生的情況相反。你在這個問題的其他地方得到了任何反饋,或者你自己取得了進展嗎?謝謝。 – Bill 2012-04-23 18:10:15
一廂情願的想法...如果你發現一個解決方案,但請發佈它。 – Aaj 2012-04-23 18:29:28