I have two ComboBoxes in my view. Both are associated with two different ObservableCollections in the ViewModel, and when the selected item in ComboBox1 is changed, ComboBox2 is updated with a different collection. Binding works very well, however I want the second ComboBox to always select the first item in its collection. It initially works, however, when the source and elements in ComboBox2 are updated, the selection index changes to -1 (i.e., the first element is no longer selected).
To fix this, I added the SourceUpdated event to ComboBox2 and the method that the event causes the index to change back to 0. The problem is that the method never gets called (I put a breakpoint at the very top of the method and it doesn’t hit). Here is my XAML code:
<Grid> <StackPanel DataContext="{StaticResource mainModel}" Orientation="Vertical"> <ComboBox ItemsSource="{Binding Path=FieldList}" DisplayMemberPath="FieldName" IsSynchronizedWithCurrentItem="True"/> <ComboBox Name="cmbSelector" Margin="0,10,0,0" ItemsSource="{Binding Path=CurrentSelectorList, NotifyOnSourceUpdated=True}" SourceUpdated="cmbSelector_SourceUpdated"> </ComboBox> </StackPanel> </Grid>
And in the code:
// This never gets called private void cmbSelector_SourceUpdated(object sender, DataTransferEventArgs e) { if (cmbSelector.HasItems) { cmbSelector.SelectedIndex = 0; } }
Any help is appreciated.
c # events data-binding wpf xaml
PoweredByOrange
source share