問題描述
我無法使用 Shift+Tab 向后導航以在包含 TextBox 的 TreeView 中工作,使用 Tab 向前導航可以正常工作并在 TreeView 內從 TextBox 跳轉到 TextBox.任何時候當 TreeView 內的 TextBox 之一使用 Shift+Tab 時,焦點就會移到 TreeView 外的上一個控件,而不是 TreeView 內的上一個控件.
I cannot get backwards navigation using Shift+Tab to work in a TreeView that contains TextBoxs, forward navigation using Tab works fine and jump from TextBox to TextBox inside the TreeView. Anytime Shift+Tab is used when one of the TextBoxes inside the TreeView, then the focus is move to the previous control outside the TreeView, instead of the previous control inside the TreeView.
此外,它唯一無法正常工作的 Shift+Tab 導航,Ctrl+Shift+Tab 按預期工作并按正確順序工作.
Also its only Shift+Tab navigation that are not working correctly, Ctrl+Shift+Tab work as expected and in the correct order.
對我做錯了什么有什么建議嗎?
Any suggestions to what I'm doing wrong?
示例代碼:
<Window x:Class="TestTabTreeView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="KeyboardNavigation.TabNavigation" Value="Continue" />
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox Text="First Line" Grid.Row="0" />
<TreeView Grid.Row="1" KeyboardNavigation.TabNavigation="Continue" IsTabStop="False">
<TreeViewItem IsExpanded="True"><TreeViewItem.Header><TextBox Text="Popular Words"/></TreeViewItem.Header>
<TreeViewItem><TreeViewItem.Header><TextBox Text="Foo"/></TreeViewItem.Header></TreeViewItem>
<TreeViewItem><TreeViewItem.Header><TextBox Text="Bar"/></TreeViewItem.Header></TreeViewItem>
<TreeViewItem><TreeViewItem.Header><TextBox Text="Hello"/></TreeViewItem.Header></TreeViewItem>
</TreeViewItem>
<TreeViewItem IsExpanded="True"><TreeViewItem.Header><TextBox Text="Unpopular Words"/></TreeViewItem.Header>
<TreeViewItem><TreeViewItem.Header><TextBox Text="Work"/></TreeViewItem.Header></TreeViewItem>
<TreeViewItem><TreeViewItem.Header><TextBox Text="Duplication"/></TreeViewItem.Header></TreeViewItem>
</TreeViewItem>
</TreeView>
<TextBox Text="Last Line" Grid.Row="2" />
</Grid>
推薦答案
如果您使用 ILSpy/Reflector 查看 TreeView.OnKeyDown 處理程序,您可以看到問題的原因.當按下 Shift+Tab 時,TreeView 有特殊處理.相關代碼為:
If you look in the TreeView.OnKeyDown handler using ILSpy/Reflector, you can see the cause of your issues. The TreeView has special handling when Shift+Tab is pressed. The relevant code is:
Key key = e.Key;
if (key != Key.Tab) {
// ...
}
else {
if (TreeView.IsShiftKeyDown && base.IsKeyboardFocusWithin &&
this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous))) {
e.Handled = true;
return;
}
}
不幸的是,您需要使用自定義 TreeView 類來解決此問題.這樣的工作:
Unfortunately, you'd need to use a custom TreeView class to work around this. Something like this works:
public class MyTreeView : TreeView {
protected override void OnKeyDown(KeyEventArgs e) {
if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0 && e.Key == Key.Tab)
return;
base.OnKeyDown(e);
}
}
這篇關于Shift+Tab 在 TreeView 控件中不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!