Data Binding to MVVM

I am using MVVM for my project and I am trying to link a table from my database using a DataGrid. But when I run my application, the datagrid is empty.

MainWindow.xaml.cs:

public MainWindow(){ InitializeComponent(); DataContext = new LecturerListViewModel() } 

MainWindow.xaml:

  <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Source=Lecturers}" > <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> <DataGridTextColumn Header="Surname" Binding="{Binding Surname}"/> <DataGridTextColumn Header="Phone" Binding="{Binding Phone_Number}" /> </DataGrid.Columns> </DataGrid> 

LecturerListViewModel.cs:

 public class LecturerListViewModel : ViewModelBase<LecturerListViewModel> { public ObservableCollection<Lecturer> Lecturers; private readonly DataAccess _dataAccess = new DataAccess(); public LecturerListViewModel() { Lecturers = GetAllLecturers(); } 

and ViewModelBase implements INotifyPropertyChanged.

Lecturer.cs

 public class Lecturer { public Lecturer(){} public int Id_Lecturer { get; set; } public string Name { get; set; } public string Surname { get; set; } public string Phone_Number { get; set; } 

What did I do wrong? I tested it with debuger and the DataContext contains all the teachers, but they are not shown in the datagrid.

+6
source share
5 answers

You have a binding error. Try the following:

 <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Lecturers}" > 

Code for:

 private ObservableCollection<Lecturer> _lecturers = new ObservableCollection<Lecturer>(); public ObservableCollection<Lecturer> Lecturers { get { return _lecturers; } set { _lecturers = value; } } 

Here is a simple code example (LecturerSimpleBinding.zip).

+8
source

Here we go

  <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Lecturers}" > 

Then

 private ObservableCollection<Lecturer> lecturers; public ObservableCollection<Lecturer> Lecturers { get { return lecturers; } set { lecturers = value; this.NotifyPropertyChanged("Lecturers"); } } 
+6
source

Saad Saad above is true. I see two potential problems with your installation, both of which are resolved by Sayed.

  • The example posted in the doen question does not implement INotifyPropertyChanged
  • A required CLR property must be PUBLIC. Fields will not work because databindindg works through reflection.
+6
source

Lecturers is a field, but data binding only works with properties. Try declaring Lecturers as:

 public ObservableCollection<Lecturer> Lecturers { get; set; } 
+2
source

MainWindow.xaml.cs : OK

MainWindow.xaml : OK

LecturerListViewModel.cs : OK - if the GetAllLecturers() method returns an ObservableCollection from Lecturer .

Lecturer.cs

 public class Lecturer : INotifyPropertyChanged { //public Lecturer(){} <- not necessary private int _id; public int Id { get { return _id; } set { _id = value; OnPropertyChanged("Id"); } } // continue doing the above property change to all the properties you want your UI to notice their changes. ... public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } 

Mark this answer: Adding INotifyPropertyChanged to the model?

+1
source

All Articles