Mike-Ward.Net

Friday Links #327

Programming

Applications

Science and Technology

On the Web

Stuff I Just Like

Favorite Tweets

  • Just did my first git push heroku master from 36k feet. Now that is true "Cloud Deployment" - Thanks @United! - James Ward

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

Friday Links #326

Programming

Applications

Science and Technology

On the Web

Stuff I Just Like

If Microsoft made a smart watch...

image

.Net, Technology, Life, Whatever

Recent Posts

Friday Links #327
WPF - Set Focus to List Box Item
Friday Links #326
Easy .NET Performance Hack
Friday Links #325
Friday Links #324
Seeing Red in Git
More... (958)