StreamT
StreamT

Reputation: 325

Setting focus on a ListBox item breaks keyboard navigation

After selecting ListBox item programmatically it is needed to press down\up key two times to move the selection. Any suggestions?

View:

<ListBox Name="lbActions" Canvas.Left="10" Canvas.Top="10"
               Width="260" Height="180">
        <ListBoxItem Name="Open" IsSelected="true" Content="Open"></ListBoxItem>
        <ListBoxItem Name="Enter" Content="Enter"></ListBoxItem>
        <ListBoxItem Name="Print" Content="Print"></ListBoxItem>
</ListBox>

Code:

public View()
{
   lbActions.Focus();
   lbActions.SelectedIndex = 0; //not helps
   ((ListBoxItem) lbActions.SelectedItem).Focus(); //not helps either
}

Upvotes: 10

Views: 11750

Answers (3)

DanielGao
DanielGao

Reputation: 1

Seems that there are two levels of Focus for ListBox control: ListBox itself and ListBoxItem. Like Heinzi said, directly set Focus for the ListBoxItem will avoid the case that you have to click twice on direction key in order to go through all ListBoxItems.

I found out this after several hours work, now it works perfect on my APP.

Upvotes: -1

Heinzi
Heinzi

Reputation: 172478

Don't set the focus to the ListBox... set the focus to the selected ListBoxItem. This will solve the "two keyboard strokes required" problem:

if (lbActions.SelectedItem != null)
    ((ListBoxItem)lbActions.SelectedItem).Focus();
else
    lbActions.Focus();

If your ListBox contains something else than ListBoxItems, you can use lbActions.ItemContainerGenerator.ContainerFromIndex(lbActions.SelectedIndex) to get the automatically generated ListBoxItem.


If you want this to happen during window initialization, you need to put the code in the Loaded event rather than into the constructor. Example (XAML):

<Window ... Loaded="Window_Loaded">
    ...
</Window>

Code (based on the example in your question):

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        lbActions.Focus();
        lbActions.SelectedIndex = 0;
        ((ListBoxItem)lbActions.SelectedItem).Focus();
    }

Upvotes: 15

Anil Vangari
Anil Vangari

Reputation: 570

You can do this easily in XAML too. Please note that this will set logical focus only.

For example:

<Grid FocusManager.FocusedElement="{Binding ElementName=itemlist, Path=SelectedItem}">
    <ListBox x:Name="itemlist" SelectedIndex="1">
        <ListBox.Items>
            <ListBoxItem>One</ListBoxItem>
            <ListBoxItem>Two</ListBoxItem>
            <ListBoxItem>Three</ListBoxItem>
            <ListBoxItem>Four</ListBoxItem>
            <ListBoxItem>Five</ListBoxItem>
            <ListBoxItem>Six</ListBoxItem>
        </ListBox.Items>
    </ListBox>
</Grid>

Upvotes: 1

Related Questions