2012-11-08 31 views
6

Projem için MVVM kullanıyorum ve veritabanımı bir DataGrid ile bir veritabanına bağlamaya çalışıyorum. Ama benim uygulamamı çalıştırdığımda datagrid boş.MVVM datagrid binding

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(); 
    } 

ve ViewModelBase INotifyPropertyChanged uygular.

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; } 

ben neyi yanlış yaptık? Ben debuger ile kontrol ettim ve DataContext tüm öğretim elemanlarını içerir, ancak datagrid'de gösterilmiyor.

cevap

7

Ciltlemede bir hata var. Bu deneyin:

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

kod arkadaki:

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

Here basit örnek kodu (LecturerSimpleBinding.zip) 'dir.

+0

Hâlâ çalışmıyor. Ama şimdi datagrid içinde sadece başlıklar var, benim çözüm ile en az boş satırlar vardı – pawel1708hp

+0

@ user1810239 Ben örnek kod oluşturdum. – kmatyaszek

+0

Çok teşekkür ederim, işe yarıyor. Sorun şu oldu: // public ObservableCollection Öğretim Elemanları; özel GözlemlenebilirDepolama _lecturers = new GözlemlenebilirKoleksiyon (); public GözlemlenebilirDepolama Okutmanlar { get {return _lecturers; } set {_lecturers = value; } } – pawel1708hp

2

Lecturers bir alandır, ancak veri bağlama yalnızca özelliklerle çalışır. gibi Lecturers bildirmeyi deneyin: İşte

public ObservableCollection<Lecturer> Lecturers { get; set; } 
6

biz

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

Sonra

private ObservableCollection<Lecturer> lecturers; 

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

Sayed Saad yukarıdaki doğru olduğunu gidin. Her ikisi de Sayed'in çözdüğü kurulumunuzla ilgili iki potansiyel sorun görüyorum.

  1. INotifyPropertyChanged
  2. CLR özelliği uygulayamaz soru DOEN gönderilmiş örnek bir KAMU MAL olmalı bağlı olması. Databindindg yansıması ile çalışır gibi alanlar çalışmayacak.
1

MainWindow.xaml.cs: MainWindow.xaml

OK:

LecturerListViewModel.cs: Tamam - GetAllLecturers() yöntem ObservableCollectionLecturer arasında döner varsayılarak.

Öğr.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)); 
     } 
    } 
} 

Kontrol Bu cevap: Adding INotifyPropertyChanged to Model?