WPF ListView在标题中带有ComboBoxes:键盘导航 [英] WPF ListView with ComboBoxes in header: keyboard navigation

查看:94
本文介绍了WPF ListView在标题中带有ComboBoxes:键盘导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在标题中有一个带有ComboBoxes的WPF ListView(用于在列表中进行过滤,请参见下面的示例).键盘导航很尴尬,我无法正确操作:

如果在选择列表中的第一项时用户按下光标(向上箭头),则焦点将移至第一列标题中的ComboBox.
然后将其困在此处,按下即可在ComboBox中移动元素.它不会将焦点移回列表.
按下TAB键可将焦点移至窗口中的下一个控件.似乎没有办法通过键盘将焦点移到列表元素上.
我想我想要的是a)在列表的第一个元素上按向上箭头"键不会将焦点转移到标题上,b)可以通过将它们与制表符分开来分别到达列表和标题部分.我似乎无法正确理解.

能做到吗?

示例代码:

I''ve got a WPF ListView with ComboBoxes in the headers (meant for filtering in the list, see sample below). Keyboard navigation is awkward and I cannot get it right:

If the user presses cursor(arrow)-up when the first item in the list is selected, focus moves to the ComboBox in the header of the first column.
And then it''s trapped there, pressing down walks the elements in the ComboBox; it does not moves focus back to the list.
Pressing TAB moves focus to the next control in the window. There doesn''t seem to be a way to get focus to the list elements anymore by keyboard.
I guess what I want is that a) pressing ''arrow up'' on the first element of the list does not transfer focus to the header, and b) that the list and header part can be separately reached by tabbing into them. I cannot seem to get this right.

Can this be done?

Sample code:

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.Header>
                    <ComboBox Width="150">
                        <ComboBoxItem Content="Filter Item 1" />
                        <ComboBoxItem Content="Filter Item 2" />
                    </ComboBox>
                </GridViewColumn.Header>
            </GridViewColumn>

            <GridViewColumn>
                <GridViewColumn.Header>
                    <ComboBox Width="150">
                        <ComboBoxItem Content="Filter Item 1" />
                        <ComboBoxItem Content="Filter Item 2" />
                    </ComboBox>
                </GridViewColumn.Header>
            </GridViewColumn>
        </GridView>
    </ListView.View>
    <ListViewItem Content="Item 1" />
    <ListViewItem Content="Item 2" />
    <ListViewItem Content="Item 3" />
    <ListViewItem Content="Item 4" />
</ListView>

<TextBox or other control />

Thanks!

推荐答案

尝试了几个小时,但从未完全正确.我最近得到的是:
Tried a few hours, never got it quite right. Closest I''ve got is:
private void testListView_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (testListView.SelectedIndex == 0)
    {
        if (e.Key == Key.Up)
        {
            e.Handled = true; // Don't move focus to the header/filter row on arrow-up.
            return;
        }
    }
}


这解决了问题的主要部分(焦点已移至向上箭头上的筛选器).

无法从另一个控件优雅地将选项卡行为放入筛选器组合框部分,然后是列表部分,再到下一个控件.没有KeyboardNavigation的组合.TabNavigation,ControlTabNavigation和DirectionalNavigation获得了所需的行为.


This fixes the main part of the problem (focus is moved to the filter on up arrow).

Couldn''t get elegantly tab behavior from another control into the filter comboboxes part, then the list part, then to the next control however. No combination of KeyboardNavigation.TabNavigation, ControlTabNavigation and DirectionalNavigation got the desired behavior.


是的,可以做到的!

您需要处理一些事件.
Yes, it can be done!

You need to work with some events.


这篇关于WPF ListView在标题中带有ComboBoxes:键盘导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆