Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
XML
<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!
Posted
Updated 21-Aug-12 4:14am
v2

Tried a few hours, never got it quite right. Closest I've got is:
C#
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;
        }
    }
}

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.
 
Share this answer
 
Yes, it can be done!

You need to work with some events.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900