cod*_*zen 3 .net wpf keyboard-events
我有以下可视树:
<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>
Run Code Online (Sandbox Code Playgroud)
handleListBoxKeyUp 如下:
private void handleListBoxKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
((UIElement)sender).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
Run Code Online (Sandbox Code Playgroud)
当ListBox有键盘焦点(实际上是ListBoxItem我猜)时,按下Enter将焦点移动到第一个ListBox而不是下面的项目TextBox.为什么会发生这种情况,我怎样才能获得Enter像Tab这里一样的关键?
MoveFocus您应该在事件args中找到的原始源上调用它,而不是调用发送方.
该sender参数将始终是ListBox自己,并呼吁MoveFocus在与FocusNavigationDirection.Next必去的树,这是第一个下一个控件ListBoxItem.
路由事件的原始源将是选定的ListBoxItem,之后的下一个控件是TextBox您想要获得焦点.
((UIElement)e.OriginalSource).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4494 次 |
| 最近记录: |