views:

35

answers:

1

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.

+1  A: 

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.

<ComboBox
    SelectedIndex="{Binding Path=Value, ElementName=_slider, Mode=TwoWay}" 
    IsEnabled="{Binding IsChecked, ElementName=_checkBox}">
    <ComboBoxItem>AAA</ComboBoxItem>
    <ComboBoxItem>BBB</ComboBoxItem>
    <ComboBoxItem>CCC</ComboBoxItem>
</ComboBox>

<Slider Name="_slider" IsSnapToTickEnabled="True" TickFrequency="1" Minimum="0" Maximum="2" Width="30"/>

<CheckBox Name="_checkBox" />

Wallstreet Programmer
In my VM there is (according to the VS search) no assignment done anywhere to the SteeringIndex property except when the screen is loaded and the setting is read from the settings file. If I put a breakpoint on that assignment and one in the property itself I only hit the one from the property and the stack trace tells me [External Code]. Which is why I find it very strange in the first place. The TwoWay binding is left from a copy paste, I'll try to remove that and try again.
tyfius
The IsEnabled twoway binding is not related to SelectedIndex so it is not the real culprit here. I just noticed you don't use a combobox directly, instead you have a user control (SettingsComboBox). Maybe you do something there to SelectedIndex.
Wallstreet Programmer
OK, it seems the problem was related to a theming issue. When IsEnabled was set to false a custom theme was applied to the ItemContainerStyle. As this is unneeded I removed the trigger and everything worked.
tyfius