我在我的視圖中有兩個組合框。它們都綁定到ViewModel中的兩個不同的ObservableCollections
,並且當ComboBox1中的所選項目發生更改時,ComboBox2會使用不同的集合進行更新。綁定工作得很好,但是,我希望第二個ComboBox始終選擇其集合中的第一個項目。但是,最初它起作用,但是,當ComboBox2中的源和項目更新時,選擇索引將更改爲-1(即,不再選擇第一個項目)。ComboBox.SourceUpdated事件沒有被解僱
爲了解決這個問題,我添加了一個SourceUpdated
事件到ComboBox2和事件調用的方法,將索引更改爲0.問題是該方法從未被調用(我在方法的最頂部放置了一個斷點,並且它不會被擊中)。這是我的XAML代碼:
<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>
而在後臺代碼:
// This never gets called
private void cmbSelector_SourceUpdated(object sender, DataTransferEventArgs e)
{
if (cmbSelector.HasItems)
{
cmbSelector.SelectedIndex = 0;
}
}
任何幫助表示讚賞。