【问题标题】:MVVM datagrid bindingMVVM 数据网格绑定
【发布时间】:2012-10-29 02:00:43
【问题描述】:

我正在为我的项目使用 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。

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

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

【问题讨论】:

    标签: c# wpf mvvm binding datagrid


    【解决方案1】:

    您在绑定时出错。试试这个:

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

    代码隐藏:

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

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

    【讨论】:

    • 还是不行。但是现在数据网格中只有标题,我的解决方案至少有空行
    • 非常感谢,它有效。这就是问题所在: //public ObservableCollection Lecturers;私人 ObservableCollection _lectures = new ObservableCollection();公共 ObservableCollection 讲师 { 获取 { 返回 _讲师; } 设置 { _讲师 = 价值; } }
    • 哦.. 是的,我专注于绑定错误,我忘记了你是如何创建这个 ObservableCollection 的。
    • @kmatyaszek 感谢您为此提供 +1 的示例,如果我想将表单中的点击事件绑定到我的 VIEWMODEL,我该如何以 selectedIndex/select 行 ID 的方式做到这一点提供给我的视图模型
    • @ArijitMukherjee 您可以在 vm 属性中创建 SelectedLecturerLecturer 的类型)并绑定到 DataGrid.SelectedItem:&lt;DataGrid ItemsSource="{Binding Lecturers}" SelectedItem="{Binding SelectedLecturer}"...&gt;,此解决方案应该可以解决您的问题。
    【解决方案2】:

    我们来了

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

    然后

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

    【讨论】:

      【解决方案3】:

      上面的 Sayed Saad 是正确的。我发现您的设置存在两个潜在问题,Sayed 解决了这两个问题。

      1. 问题中发布的示例未实现 INotifyPropertyChanged
      2. 要绑定的 CLR 属性必须是 PUBLIC PROPERTY。字段将不起作用,因为数据绑定通过反射起作用。

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          MainWindow.xaml.cs:好的

          MainWindow.xaml:好的

          LecturerListViewModel.cs:好的 - 假设 GetAllLecturers() 方法返回 ObservableCollectionLecturer

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

          检查这个答案: Adding INotifyPropertyChanged to Model?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-30
            • 2014-01-29
            • 1970-01-01
            相关资源
            最近更新 更多