I am creating an application using the MVVM pattern and somewhere I have a view which contains a ComboBox.
<Controls:SettingsComboBox x:Name="Setting"
SelectedIndex="{Binding SteeringIndex, Mode=TwoWay}"
IsEnabled="{Binding SteerAttached, Mode=TwoWay}">
<ComboBoxItem Content="{Binding Path=on, Source={x:Static Localization:CultureResources.ObjectDataProvider}}"/>
<ComboBoxItem Content="{Binding Path=off, Source={x:Static Localization:CultureResources.ObjectDataProvider}}"/>
</Controls:SettingsComboBox>
So the SelectedIndex and IsEnabled are bound to the view model which retrieves the values from a settings file or gets it from an external event.
If SelectedIndex is 0 (on) and IsEnabled is set to false, everything is OK. If SelectedIndex is 1 (off) and IsEnabled is set to false the SelectedIndex is somehow set to -1, thus resulting in an empty combo box.
Why does this happen and how can I work around it? I can set the SelectedIndex back when I receive the event to change the IsEnabled property, but that still leaves and empty combobox when it’s disabled so that doesn’t really qualify as a solution.
Setting IsEnabled on a combobox does not change its SelectedIndex property. Something in your code (not shown here) is doing that, probably your VM. Question, why did you do twoway binding for IsEnabled? Do you really want to update SteerAttached whenever IsEnabled on the combobox changes?
Below code lets you play around with enabling/disabling a combobox and changing selected index. As you can see these properties works independent of each other.