8

我正在为我的项目使用 MVVM,并且正在尝试将数据库中的表与 DataGrid 绑定。但是当我运行我的应用程序时,数据网格是空的。

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

并且 ViewModelBase 实现了 INotifyPropertyChanged。

讲师.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; }

我做错什么了?我用调试器检查了它,DataContext 包含所有讲师,但数据网格中没有显示。

4

5 回答 5

10

你有一个绑定错误。试试这个:

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

代码隐藏:

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

是简单的示例代码 (LecturerSimpleBinding.zip)。

于 2012-11-08T19:59:38.957 回答
8

开始了

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

然后

private ObservableCollection<Lecturer> lecturers;

public ObservableCollection<Lecturer> Lecturers
{
    get { return lecturers; }
    set
    {
        lecturers = value;
        this.NotifyPropertyChanged("Lecturers");
    }
}
于 2012-11-08T20:23:21.650 回答
6

上面的赛义德萨阿德是正确的。我发现您的设置存在两个潜在问题,Sayed 都解决了这两个问题。

  1. 问题中发布的示例未实现 INotifyPropertyChanged
  2. 绑定到的 CLR 属性必须是 PUBLIC PROPERTY。字段将不起作用,因为数据绑定通过反射工作。
于 2012-11-09T02:35:19.397 回答
2

Lecturers是一个字段,但数据绑定仅适用于属性。尝试声明Lecturers如下:

public ObservableCollection<Lecturer> Lecturers { get; set; }
于 2012-11-08T20:18:03.303 回答
1

MainWindow.xaml.cs:好的

MainWindow.xaml:好的

LecturerListViewModel.cs:好的——假设该GetAllLecturers()方法返回一个ObservableCollectionof 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));
        }
    }
}

检查这个答案: 将 INotifyPropertyChanged 添加到模型?

于 2015-02-27T16:13:45.760 回答