title: How can I set my comboBox values to a default value that they contain?
body: <p>I've tried both of these:</p>
  
  <pre><code>comboBoxToHour.SelectedValue = comboBoxToHour.Items.IndexOf("11");
  comboBoxToHour.SelectedIndex = comboBoxToHour.Items.IndexOf("11");
  </code></pre>
  
  <p>...and neither one works and, in fact, neither does this:</p>
  
  <pre><code>comboBoxFromHour.SelectedValue = "11";
  </code></pre>
  
  <p>...nor this:</p>
  
  <pre><code>comboBoxFromHour.SelectedIndex = 12;
  </code></pre>
  
  <p>...which I find really odd.</p>
  
  <h2>UPDATE</h2>
  
  <p>I tried Ross' suggested fix, but with this:</p>
  
  <pre><code>if (App.roamingSettings.Values.ContainsKey("FromMonthSection1"))
  {
      if (comboBoxFromMonth.Items != null)
      {
          var item = comboBoxFromMonth.Items.FirstOrDefault(p =&gt; (p as ComboBoxItem).Content.ToString() == App.roamingSettings.Values["FromMonthSection1"].ToString());
  </code></pre>
  
  <p>...I get, "Object reference not set to an instance of an object." on the last line.</p>
  
  <h2>UPDATE 2</h2>
  
  <p>As it turns out, a simple way works after all. It turns out that this:</p>
  
  <pre><code>if (App.roamingSettings.Values.ContainsKey("FromMonthSection1"))
  {
      if (comboBoxFromMonth != null)
      {
          var item = comboBoxFromMonth.Items.FirstOrDefault(p =&gt; (p as ComboBoxItem).Content.ToString() == App.roamingSettings.Values["FromMonthSection1"].ToString());
          if (item != null)
          {
              comboBoxFromMonth.SelectedItem = item;
          }
      }
  }
  </code></pre>
  
  <p>...fails with, "<em>System.NullReferenceException was unhandled by user code
    HResult=-2147467261
    Message=Object reference not set to an instance of an object.</em>"</p>
  
  <p>...on the line assigning to item.</p>
  
  <p>However, this:</p>
  
  <pre><code>if ((App.roamingSettings.Values.ContainsKey("FromMonthSection1")) &amp;&amp;
      (!string.IsNullOrWhiteSpace(App.roamingSettings.Values["FromMonthSection1"].ToString())))
  {
      comboBoxFromMonth.SelectedItem = App.roamingSettings.Values["FromMonthSection1"].ToString();
  }
  </code></pre>
  
  <p>...works.</p>