将ViewModel子属性绑定到DataGridView列

时间:2017-03-22 05:08:29

标签: c# winforms datagridview reactiveui

我有一个带有Model属性的ViewModel:

public AnimalViewModel : ReactiveObject
{
  private Animal model;
  public Animal Model
  {
    get { return model; }
    set { this.RaiseAndSetIfChanged(ref model, value); }
  }

  private String rowError;
  public String RowError
  {
    get { return rowError; }
    set { this.RaiseAndSetIfChanged(ref rowError, value); }
  }

  public ViewModel( Animal _model )
  {
    Model = _model;
  }
}

然后我有一个ViewModel集合:

public AnimalsViewModel : ReactiveObject
{
  private ReactiveList<Animal> animals;
  public ReactiveList<Animal> Animals
  {
    get { return animals; }
    set { this.RaiseAndSetIfChanged(ref animals, value); }
  }

  private IReactiveDerivedList<AnimalViewModel> animalViewModels;
  public IReactiveDerivedList<AnimalViewModel> AnimalViewModels
  {
    get { return animalViewModels; }
    set { this.RaiseAndSetIfChanged(ref animalViewModels, value); }
  }

  public ViewModel()
  {
    Animals = new ReactiveList<Animal>();
    AnimalViewModels = Animals.CreateDerivedCollection( x => new AnimalViewModel(x) );
    Model = _model;
  }
}

在Winforms中,我想将DataGridView.DataSource绑定到AnimalViewModels。这将让我利用RowError属性。但是,网格内容的字段现在位于子属性Model下。

在设计器中,我将DataPropertyName列设为Model.SomeAnimalField。虽然代码运行,但DataGridView单元格为空。有没有办法做到这一点?

0 个答案:

没有答案