我有一个带有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
单元格为空。有没有办法做到这一点?