Mike-Ward.Net

WPF - Set Focus to List Box Item

Yes, I’m writing a post on how to set the keyboard focus to the first item of a list-box in WPF. And it only took an hour to find a solution. Not that I’m bitter but, “Holy %*@#!”, why does something so simple require 15 lines of code!

public RecentFilesDialog()
{
    InitializeComponent();
    FilesListBox.ItemsSource = Settings.Default.RecentFiles.Cast<string>().Reverse().Skip(1);
    FilesListBox.ItemContainerGenerator.StatusChanged += ItemContainerGeneratorOnStatusChanged;
}

private void ItemContainerGeneratorOnStatusChanged(object sender, EventArgs eventArgs)
{
    if (FilesListBox.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
    {
        var index = FilesListBox.SelectedIndex;
        if (index >= 0)
        {
            var item = FilesListBox.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;
            if (item != null) item.Focus();
        }
    }
}

You’ll see lots of articles about using FocusManager on an element. Doesn’t work. Focus goes to the control, not the item.

This code recognizes that ItemSource loads asynchronously and waits until ItemContainerGenerator has finished before trying to set keyboard focus.

Reference: Selecting first item in WPF ListView and keyboard navigation

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

Two Handy Command Line Scripts
Tweetz 1.3.2
Tweetz 1.3 Release
Mithril Rising
Tweetz Release 1.2.4 - Screen Off Detection
Tweetz Desktop Support for 280 Characters
Quick Tip - Clear ReSharper's Cache to Fix False Errors
More... (1083)

Donate