I have a ViewModel which exposes a DataSource which is an ObservableCollection of DynamicObjects. Upon binding, the DataGrid calls GetDynamicMemberNames() on the first DataSource item to obtain the columns it needs to autogenerate and bind to. So far so good.
However, when I then change the DataSource to contain items with completely different properties and raise PropertyChanged for the DataSource, the Grid does not re-evaluate the dynamic members!
My question is, how do I get the DataGrid to re-evaluate the DynamicObject's members? How do I force it to call GetDynamicMemberNames after the initial binding?
Some code:
private ObservableCollection<dynamic> _dataSource;
public ObservableCollection<dynamic> DataSource
{
get
{
if(_dataSource == null)
{
_dataSource = new ObservableCollection<dynamic>();
foreach(var model in SourceModels)
{
var row = new DynamicDataRow() // Inherits from DynamicObject ...
row["SomeProperty"] = model.GetType().GetProperty("SomeProperty").GetValue(model, null);
_dataSource.Add(row);
}
}
return _dataSource;
}
}
This works if I fill the SourceModels collection in the ViewModel constructor.
What I'm looking for is some way to rebind the grid in a way that calls GetDynamicMemberNames() after I change the SourceModels collection. Preferably in an MVVM manner...
Can anybody help me out?