I am trying to bind the SelectedIndex property of combobox to my ViewModel. Here is the code.
Xaml:
<ComboBox x:Name="BloodGroupFilter" SelectedIndex="{Binding Path=SelectedBloodGroupIndex, Mode=TwoWay}">
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem Foreground="red" FontStyle="Italic">No Filter</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource BloodGroupEnum}}"/>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
ViewModel
private int _selectedBloodGroupIndex = 4;
public int SelectedBloodGroupIndex {
get { return _selectedBloodGroupIndex; }
set {
_selectedBloodGroupIndex = value;
}
}
As you can see I am trying to set the SelectedIndex of combobox to "4". This doesn't happen and SelectedIndex is set to 0. Also, when user selects a particular item of the combobox, I was expecting that the ViewModel's SelectedBloodGroupIndex property will update itself to the currently selected item of combobox, but this doesn't happen either. The ViewModel property is never invoked(both set and get). Any reasons why binding is failing for the above code.
Update
<UserControl.Resources>
<ObjectDataProvider x:Key="BloodGroupEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="enums:BloodGroup" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>