title: How to programmatically set SelectedItem of a data-bound WPF ComboBox?
body: <p><strong>Question:</strong> Can anyone please provide a full code example that shows how one does programmatically change the SelectedItem of a data-bound WPF ComboBox without using MyComboBox.SelectedIndex?</p>
  
  <p><strong>Code sample:</strong> Here is what I currently have.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="Wpf.ComboBoxDemo.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto"/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;ComboBox Name="MyComboBox" DisplayMemberPath="LastName" SelectedIndex="0"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Code-behind:</p>
  
  <pre><code>using System.Collections.ObjectModel;
  using System.Windows;
  
  namespace Wpf.ComboBoxDemo
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
  
              ObservableCollection&lt;Person&gt; myPersonList = new ObservableCollection&lt;Person&gt;();
  
              Person personJobs = new Person("Steve", "Jobs");
              Person personGates = new Person("Bill", "Gates");
  
              myPersonList.Add(personJobs);
              myPersonList.Add(personGates);
  
              MyComboBox.ItemsSource = myPersonList;
  
              // How do I programmatically select the second Person, i.e. "Gates"?
              // The best pratice must be to somehow to set something like IsCurrentlySelected on the model, so the view update automatically. But how?
              MyComboBox.SelectedIndex = 1; // This works, but is there no way without using the index?
  
          }
  
          private class Person
          {
              public string FirstName { get; set; }
              public string LastName { get; set; }
  
              public Person(string firstName, string lastName)
              {
                  FirstName = firstName;
                  LastName = lastName;
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Similar questions:</strong> I have of course searched the Internet first, but found nothing that helped me.</p>
  
  <ul>
  <li>Changing the SelectedItem of a enum-bound combobox inside ViewModel (<a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/371c663b-a136-43ab-9006-fb5697a815f5" rel="nofollow noreferrer">MSDN</a>)</li>
  <li>Programmatically set ComboBox SelectedItem in WPF (3.5sp1) (<a href="https://stackoverflow.com/questions/2234063/programmatically-set-combobox-selecteditem-in-wpf-3-5sp1">Stack Overflow</a>) </li>
  </ul>