Click here to Skip to main content
15,911,142 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
I am trying to read the values of a column from a database table, store those values as a
List<string>
and bind it to a combobox in WPF using MVVM.

Although the values are read from the database and stored in a
List<string>
, the ComboBox does not display it as a dropdown.

I have been stuck with this problem for 2 days.
Any Help would be truely appreciated.

Here is my code:

ViewModel.cs:
C#
private List<string> myComboBoxData =null;

        public List<string> MyComboBoxData
        {
           get
           {
               return this.myComboBoxData ;
           }
           set
           {
               this.myComboBoxData = value;
               base.OnPropertyChanged("MyComboBoxData ");
           }
        }

        public ViewModel(UserDetails view)
        {
            this.view = view;
            view.DataContext = this;
            getmyComboBoxData();
        }

        private void getmyComboBoxData()
        {
          using (myEntities context = new myEntities())
            {
                myComboBoxData = (from utype in context.usertypes
                           select utype.TypeName).ToList<string>();
            }
        }


View.xaml:
<ComboBox  ItemsSource="{Binding MyComboBoxData}" Height="23" HorizontalAlignment="Left" Margin="150,91,0,0" Name="mycomboBox" VerticalAlignment="Top" Width="120" />
Posted
Updated 24-Mar-13 2:18am
v2
Comments
[no name] 24-Mar-13 9:06am    
In getmyComboBoxData you are getting your data and you are putting the data in the list but the view is never notified that the list has changed. You either need to do an OnPropertyChanged in getmyComboBoxData or in getmyComboBoxData change myComboBoxData to MyComboBoxData

1 solution

In the public List<string> MyComboBoxData setter, the argument of base.OnPropertyChanged should be exact name of the property - "MyComboBoxData" - capital M and no space.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900