我有以下视觉树:
<DockPanel>
<TextBox Name="ElementWithFocus" DockPanel.Dock="Left" />
<ListBox DockPanel.Dock="Left" Width="200" KeyUp="handleListBoxKeyUp">
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>4</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
</ListBox>
<TextBox DockPanel.Dock="Left" />
</DockPanel>
handleListBoxKeyUp
如下:
private void handleListBoxKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
((UIElement)sender).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
当ListBox
具有键盘焦点时(ListBoxItem
我猜真的是这样),按下Enter
会将焦点移动到中的第一项ListBox
而不是下一项TextBox
。为什么会发生这种情况,我怎样才能获得Enter
像这里一样行事的钥匙Tab
?