如何影响 WPF 中的键盘焦点遍历策略?
How to influence keyboard focus traversal policy in WPF?
假设有两个 WPF 列表。
我希望能够使用键盘箭头键无缝地遍历它列出的项目。
默认情况下,焦点范围仅限于单个容器内的迭代。
有什么办法可以帮助它跨越集装箱边界吗?
假设有两个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;
}
}
这将允许使用 up/down 键在两个 ListView 之间进行无缝垂直过渡。您可以添加更多案例块以添加额外的 left/right 转换或其他内容。
我相信您可以想出如何使它适应您的具体情况。
刚刚弄明白了。
KeyboardNavigation.DirectionalNavigation attached property does the trick. (possible values listed here)
示例可以简化为:
<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>
假设有两个 WPF 列表。 我希望能够使用键盘箭头键无缝地遍历它列出的项目。 默认情况下,焦点范围仅限于单个容器内的迭代。 有什么办法可以帮助它跨越集装箱边界吗?
假设有两个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;
}
}
这将允许使用 up/down 键在两个 ListView 之间进行无缝垂直过渡。您可以添加更多案例块以添加额外的 left/right 转换或其他内容。 我相信您可以想出如何使它适应您的具体情况。
刚刚弄明白了。 KeyboardNavigation.DirectionalNavigation attached property does the trick. (possible values listed here)
示例可以简化为:
<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>