ComboBox Lost SelectedIndex

I had a problem when my combobox loses the value of SelectedIndex when closing UserControl. The ViewModel still has this, but the view continues to reset it to -1. I understand that there is a problem with the binding order of ItemSource and SelectedIndex, but I am not binding directly to ItemSource. Basically, I'm trying to figure out the correct syntax for the binding below.

</ComboBox.ItemTemplate> <ComboBox.ItemsSource> <CompositeCollection> <ComboBoxItem IsEnabled="False">Select a database connection...</ComboBoxItem> <CollectionContainer Collection="{Binding Source={StaticResource ConnectionsBridge}}" /> <ComboBoxItem>&lt;New...&gt;</ComboBoxItem> </CompositeCollection> </ComboBox.ItemsSource> **<ComboBox.SelectedIndex> <Binding Path="SelectedConnectionIndex"/> </ComboBox.SelectedIndex>** </ComboBox> 
+1
source share
1 answer

You are attached to an index (int) or element (object). Your example is bound to a property that indicates an index, not an object.

You must set the Mode property to bind SelectedIndex

 <ComboBox SelectedIndex="{Binding SelectedConnectionIndex, Mode=TwoWay}"> </ComboBox> 
+1
source

Source: https://habr.com/ru/post/1483596/


All Articles