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

上記のSaadは正しいです。あなたのセットアップには 2 つの潜在的な問題があると思いますが、どちらも 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 : OK

MainWindow.xaml : OK

LecturerListViewModel.cs : OK - メソッドがof をGetAllLecturers()返すと仮定します。ObservableCollectionLecturer

講師.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 に答える