Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is in your <code>this.Combinations.Clear();</code><br> When you do it, it sets <code>SelectedItem = null</code> and <code>SelectedIndex = -1</code> </p> <p>So you should set selection again. If you have an access to your ComboBox then just write this <code>cbCombination.SelectedIndex = 0;</code> after filling Combinations list.<br> Of course you can bind SelectedItem/SelectedIndex to some property in code behind too.</p> <p>BTW<br> Also it is not required to call <code>NotifyPropertyChanged(@"Combinations");</code> after filling the collection because <code>Combinations</code> already implements <code>INotifyPropertyChanged</code>.</p> <p><strong>Update</strong></p> <p>To detect that your <em>ObservableCollection</em> was changed, subscribe to <em>CollectionChanged</em> event in your <em>UserControl</em> code behind. Make sure that you subscribed <strong>before</strong> collection changed!</p> <pre><code>Combinations.CollectionChanged += (s, e) =&gt; { if (cbCombination.Items.Count &gt; 0) cbCombination.SelectedIndex = 0; }; </code></pre> <p><strong>Another suggestion</strong></p> <p>Approach above is works when you do not need a smarter logic than just select zero index in combobox. But often you will need a more complex logic to select some item. In this case you may add new property to your model: </p> <pre><code>public Combination SelectedCombination { get{ return _selectedCombination; } set { _selectedCombination = value; NotifyPropertyChanged("SelectedCombination"); } } </code></pre> <p>and bind this property to your combobox:</p> <pre><code>&lt;ComboBox Name="cbCombination" ItemsSource="{Binding Combinations}" SelectedIndex="0" SelectedItem={Bindings SelectedCombination} /&gt; </code></pre> <p>In this case you can select any item when filling the Combinations collection and it will be automatically selected in combobox:</p> <pre><code>var combinationsList = CombinationsManager.CombinationsFor(someParam); this.Combinations.Clear(); foreach (var combination in combinationsList) this.Combinations.Add(combination); if (Combinations.Count &gt; 0) SelectedCombination = Combinations[0]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload