Jatin
Jatin

Reputation: 4063

WPF Combobox SelectedIndex Property Binding Not working

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>

Upvotes: 2

Views: 15409

Answers (1)

yo chauhan
yo chauhan

Reputation: 12305

You need to Notify Property changed in the setter of SelectedBloodGroupIndex of your ViewModel . I hope you do have the idea of PropertyChanged event.

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:myWindow="clr-namespace:WpfApplication4"
    Title="MainWindow" Height="800" Width="800" WindowStartupLocation="CenterScreen">

<Grid>
    <ComboBox SelectedIndex="{Binding SelectedIndex}">
        <ComboBoxItem Content="1"/>
        <ComboBoxItem Content="2"/>
        <ComboBoxItem Content="3"/>
        <ComboBoxItem Content="4"/>
        <ComboBoxItem Content="5"/>
    </ComboBox>
</Grid>

 public partial class MainWindow :Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MyViewModel();
    }
}

public class MyViewModel :INotifyPropertyChanged
{
    public MyViewModel()
    {
        SelectedIndex = 2;
    }
    private int _selectedIndex;
    public int SelectedIndex 
    { 
        get
        {
            return _selectedIndex;
        }
        set
        {
            _selectedIndex = value;
            Notify("SelectedIndex");
        }
  }

    public event PropertyChangedEventHandler PropertyChanged;

    private void Notify(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Upvotes: 4

Related Questions