title: refresh combobox items at runtime wpf
body: <p>I want a comboBox containing open serial ports as items. But it updates automatically when serial ports modify. </p>
  
  <p>My approach is this: </p>
  
  <ol>
  <li><p>create in Xaml a comboBox like this:</p>
  
  <pre><code>&lt;ComboBox Height="23" HorizontalAlignment="Left" Margin="87,35,0,0" Name="comboBox1" VerticalAlignment="Top" Width="147" ContextMenuOpening="comboBox1_ContextMenuOpening" /&gt;
  </code></pre></li>
  <li><p>create a method that loops trough ports and adds them to comboBox:</p>
  
  <pre><code>public string[] portsManual;
  
  public void adaugaPorturi()
  {
      if (comboBox1.Items.Count &gt; 0)
      {
          comboBox1.Items.RemoveAt(0);
      }
      comboBox1.Items.Add("Select port");
      comboBox1.SelectedItem = "Select port";
      portsManual = SerialPort.GetPortNames();
      foreach (string port in portsManual)
      {
          comboBox1.Items.Add(port);
      }
      comboBox1.Items.Refresh();
      Array.Clear(portsManual, 0, portsManual.Length);
  }
  </code></pre></li>
  <li><p>call this method at window.load (so it can have something in it - the ports opened right then) and call it at ContextMenuOpening event (whenever I click the comboBox to select an item - a port, to refresh)</p></li>
  </ol>
  
  <p>Note: I am clearing the comboBox items (if any) at start, and the same for the string array, but it doesnt update, the items are still the ones at first run of this method!</p>
  
  <p>I have tried: ObservableCollection, strings, and arrays with no luck. The most info I have found on this site. I am thinking that because are only a few ports, I don't really need a Collection or List. I just add items directly.</p>
  
  <p>BTW, can an admin edit this post? I am not sure if text format is good.</p>