假设有两个WPF列表。我希望能够无缝地迭代它所列出的两个项目,只使用键盘箭头键。默认情况下,焦点范围仅限于单个容器中的迭代。有什么办法帮助它跨越集装箱边界吗?
发布于 2016-06-20 09:54:36
我刚想明白了。KeyboardNavigation.DirectionalNavigation附加属性可以完成此任务。(可能的值列出了这里
可将样本简化为:
<StackPanel KeyboardNavigation.DirectionalNavigation="Contained">
<ListBox KeyboardNavigation.DirectionalNavigation="Continue">
<ListBoxItem Content="Item 1" />
<ListBoxItem Content="Item 2" />
<ListBoxItem Content="Item 3" />
</ListBox>
<ListBox KeyboardNavigation.DirectionalNavigation="Continue">
<ListBoxItem Content="Item A" />
<ListBoxItem Content="Item B" />
<ListBoxItem Content="Item C" />
</ListBox>
</StackPanel>
发布于 2016-06-18 13:46:33
因此,假设有两个ListViews:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ListView x:Name="list1" KeyDown="list1_KeyDown">
<ListViewItem Content="item 1"/>
<ListViewItem Content="item 2"/>
<ListViewItem Content="item 3"/>
</ListView>
<ListView x:Name="list2" Grid.Row="1" KeyDown="list2_KeyDown">
<ListViewItem Content="item 1"/>
<ListViewItem Content="item 2"/>
<ListViewItem Content="item 3"/>
</ListView>
</Grid>
为KeyDown事件添加两个处理程序:
private void list1_KeyDown(object sender, KeyEventArgs e) {
switch (e.Key) {
// check for down key
case Key.Down:
// if the bottom item is selected
if (list1.SelectedIndex == list1.Items.Count - 1) {
// select the next item
list2.SelectedIndex = 0;
(list2.Items[0] as UIElement).Focus();
// make sure nothing else happens
e.Handled = true;
}
break;
}
}
private void list2_KeyDown(object sender, KeyEventArgs e) {
switch (e.Key) {
// check for up key
case Key.Up:
// if the top item is selected
if (list2.SelectedIndex == 0) {
// select the previous item
int i = list1.Items.Count - 1;
list1.SelectedIndex = i;
(list1.Items[i] as UIElement).Focus();
// make sure nothing else happens
e.Handled = true;
}
break;
}
}
这将允许使用向上/向下键在两个ListViews之间无缝垂直转换。您可以添加更多的case块来添加更多的左/右转换或其他任何东西。我相信你能想出如何使它适应你的具体情况。
https://stackoverflow.com/questions/37886435
复制相似问题