title: How do you programmatically set focus to the SelectedItem in a WPF ListBox that already has focus?
body: <p>We want to set the <code>SelectedItem</code> of a <code>ListBox</code> programmatically and want that item to then have focus so the arrow keys work relative to that selected item.  Seems simple enough.</p>
  <p>The problem however is if the <code>ListBox</code> already has keyboard focus when setting <code>SelectedItem</code> programmatically, while it does properly update the <code>IsSelected</code> property on the <code>ListBoxItem</code>, it <em>doesn't</em> set keyboard focus to it, and thus, the arrow keys move relative to the previously-focused item in the list and not the newly-selected item as one would expect.</p>
  <p>This is very confusing to the user as it makes the selection appear to jump around when using the keyboard as it snaps back to where it was before the programmatic selection took place.</p>
  <p>Note: As I said, this only happens if you programmatically set the <code>SelectedItem</code> property on a <code>ListBox</code> that already has keyboard focus itself.  If it doesn't (or if it does but you leave, then come right back), when the keyboard focus returns to the <code>ListBox</code>, the correct item will now have the keyboard focus as expected.</p>
  <p>Here's some sample code showing this problem.  To demo this, run the code, use the mouse to select 'Seven' in the list (thus putting the focus on the <code>ListBox</code>), then click the 'Test' button to programmatically select the fourth item.  Finally, tap the 'Alt' key on your keyboard to reveal the focus rect.  You will see it's still on 'Seven', not 'Four' as you may expect, and because of that, if you use the up and down arrows, they are relative row 'Seven', not 'Four' as well, further confusing the user since what they are visually seeing and what is actually focused are not in sync.</p>
  <blockquote>
  <p>Important: Note that I have <code>Focusable</code> set to <code>false</code> on the button.  If I didn't, when you clicked it, it would gain focus and the <code>ListBox</code> would lose it, masking the issue because again, when a <code>ListBox</code> regains focus, it properly focuses the selected <code>ListBoxItem</code>. The issue is when a <code>ListBox</code> <em>already</em> has focus and you programmatically select a <code>ListBoxItem</code>.</p>
  </blockquote>
  <p>XAML file:</p>
  <pre><code>&lt;Window x:Class=&quot;Test.MainWindow&quot;
      xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
      Width=&quot;525&quot; Height=&quot;350&quot; WindowStartupLocation=&quot;CenterScreen&quot;
      Title=&quot;MainWindow&quot; x:Name=&quot;Root&quot;&gt;
  
      &lt;DockPanel&gt;
  
          &lt;Button Content=&quot;Test&quot;
              DockPanel.Dock=&quot;Bottom&quot;
              HorizontalAlignment=&quot;Left&quot;
              Focusable=&quot;False&quot;
              Click=&quot;Button_Click&quot; /&gt;
  
          &lt;ListBox x:Name=&quot;MainListBox&quot; /&gt;
  
      &lt;/DockPanel&gt;
  
  &lt;/Window&gt;
  </code></pre>
  <p>Code-behind:</p>
  <pre><code>using System.Collections.ObjectModel;
  using System.Windows;
  
  namespace Test
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
  
              MainListBox.ItemsSource = new string[]{
                  &quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot;, &quot;Five&quot;, &quot;Six&quot;, &quot;Seven&quot;, &quot;Eight&quot;
              };
  
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              MainListBox.SelectedItem = MainListBox.Items[3];
          }
  
      }
  
  }
  </code></pre>
  <p>Note: Some have suggested to use <code>IsSynchronizedWithCurrentItem</code>, but that property synchronizes the <code>SelectedItem</code> of the <code>ListBox</code> with the <code>Current</code> property of the associated view.  It isn't related to focus as this problem still exists.</p>
  <p>Our work-around is to temporarily set the focus somewhere else, then set the selected item, then set the focus back to the <code>ListBox</code> but this has the undesireable effect of us having to make our <code>ViewModel</code> aware of the <code>ListBox</code> itself, then perform logic depending on whether or not it has the focus, etc. (i.e. you wouldn't want to simply say 'Focus elsewhere then come back here, if 'here' didn't have the focus already as you'd steal it from somewhere else.)  Plus, you can't simply handle this through declarative bindings.  Needless to say this is ugly.</p>
  <p>Then again, 'ugly' ships, so there's that.</p>