0
votes

I am trying to bind WPF combobox ItemsSource with a collection using MVVM design pattern. following is my code

XAML:

<ComboBox Height="30" Width="200" ItemsSource="{Binding PeopleList,Mode=TwoWay}"></ComboBox>
<TextBlock Height="Auto" Width="Auto" Text="{Binding SelectedPerson.ContactNo}"></TextBlock>

Code Behind :

public MainWindow()
        {
            InitializeComponent();
            ViewModel vm = new ViewModel();
            DataContext = vm;
        }

Model Class :

class People : INotifyPropertyChanged
    {
        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }

        private string contactNo;

        public string ContactNo
        {
            get { return contactNo; }
            set
            {
                contactNo = value;
                NotifyPropertyChanged("ContactNo");
            }

        }

        private ObservableCollection<People> peopleList;

        public ObservableCollection<People> PeopleList
        {
            get { return peopleList; }
            set
            {
                peopleList = value;
                NotifyPropertyChanged("PeopleList");
            }
        }

        private People selectedPerson;

        public People SelectedPerson
        {
            get { return selectedPerson; }
            set
            {
                selectedPerson = value;
                NotifyPropertyChanged("SelectedPerson");
            }
        }




        public event PropertyChangedEventHandler PropertyChanged;

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

ViewModel Class :

class ViewModel
           {
        ObservableCollection<People> PeopleList = null;
        public ViewModel()
        {
            PeopleList = new ObservableCollection<People>();
            People p1 = new People { Name = "Naresh", ContactNo = "9574733355" };
            People p2 = new People { Name = "Ritesh", ContactNo = "9099028779" };
            People p3 = new People { Name = "Saumya", ContactNo = "9904848779" };

            PeopleList.Add(p1);
            PeopleList.Add(p2);
            PeopleList.Add(p3);

            People People = new People();
            People.PeopleList = PeopleList;

        }

So, this is what I have done so far. Here, the issue I am facing is when I click on combo box nothing is happening.

Thanks for your help in advance.

2
Any binding errors in the output window?bdimag

2 Answers

2
votes

The PeopleList needs to be real property (not a local field):

public ObservableCollection<People> PeopleList { get; set; }
0
votes

You have to replace the field entry

ObservableCollection<People> PeopleList = null;

by a property entry

public ObservableCollection<People> PeopleList { get; set; }

Only properties can be binded not fields.

Furthermore you should set the DisplayMemberPath to show more than the type name "People". The three items cannot be distinguished. You could show the names of the people

        <ComboBox Height="30" Width="200" ItemsSource="{Binding PeopleList,Mode=TwoWay}" DisplayMemberPath="Name"></ComboBox>