Note that there are some explanatory texts on larger screens.

plurals
  1. POCombo-box loses selection after collection changes
    primarykey
    data
    text
    <p>I have a WPF combo box bound to an obvserable collection (OC):</p> <pre><code> &lt;ComboBox Name="cbCombination" ItemsSource="{Binding Combinations}" SelectedIndex="0" /&gt; </code></pre> <p>Elsewhere, in the object set as data context:</p> <pre><code> public ObservableCollection&lt;Combination&gt; Combinations { get; set; } </code></pre> <p>Combination overrides its ToString and everything is peachy: The combo-box's drop-down displays all Combination items in the Combinations OC. The combo-box's selection box displays the value of the first Combination.</p> <p>Now, the data-context object has to change the values in its Combinations OC:</p> <pre><code> var combinationsList = CombinationsManager.CombinationsFor(someParam); this.Combinations.Clear(); foreach (var combination in combinationsList) this.Combinations.Add(combination); NotifyPropertyChanged(@"Combinations"); </code></pre> <p>This causes the combo-box's selection box shows an empty string. (The drop-down is closed. However, when I make it drop down, it does show the correct new Combinations, so it <em>is</em> bound to the updated collection).</p> <p>I tried to capture both SourceUpdated and (in my dispair) TargetUpdated events (thining of setting the SelectedIndex there), but my event handlers didn't get called!</p> <p>So my question is: <strong>How do I make a WPF ComboBox refresh the value of its selection-box when the observable collection it is bound-to changes?</strong></p> <p><strong>Update</strong>: </p> <p>I've totally forgotten to mention, and I don't know whether it's important, but the combo-box is within a UserControl. The UserControl's XAML looks like this:</p> <pre><code>&lt;UserControl x:Class="...CombinationsToolBar" .... mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"&gt; &lt;ToolBarTray Name="toolBarTray1" &gt; &lt;ToolBar Name="toolBar1"&gt; &lt;ComboBox Name="cbCombination" ItemsSource="{Binding Path=Combinations, NotifyOnSourceUpdated=True, IsAsync=True}" SelectedIndex="0" IsEditable="False" SelectionChanged="CbCombinationSelectionChanged" SourceUpdated="CbCombinationSourceUpdated" TargetUpdated="CbCombinationTargetUpdated"&gt;&lt;/ComboBox&gt; &lt;/ToolBar&gt; &lt;/ToolBarTray&gt; </code></pre> <p></p> <p><strong>In the UserControl's code</strong> I have breakpoints on CbCombinationSelectionChanged, CbCombinationSourceUpdated and CbCombinationTargetUpdated. </p> <p>The CbCombinationSelectionChanged fires once when the form containing the user control is first loaded. It is indeed called a second time when the Combinations collection is cleared, as @asktomsk said.</p> <p>The source updated and target updated are not triggered - CbCombinationSourceUpdated and CbCombinationTargetUpdated are not called. </p> <p>As the combo box is inside a usercontrol and the Combinations collection is within the view model, and as the view model doesn't have access to the combo box, <strong>I have no opportunity to set the selected index of the combo unless the events fire</strong>.</p> <p>:(</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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