Hi Walter,
What is BindableDynamicDictionary? I tried looking around but this appears to be a custom type.
Are you replacing the ObservableCollection instance in your view model and triggering a property changed notification? If the new data source has properties that the previous XamDataGrid FieldLayout is unaware of then it should generate a new FieldLayout. I don't know how your BindableDynamicDictionary works exactly so I can't be 100% sure. If this is one of your own custom types can you show me the code for this class?
Sorry, that class came from a post on Stack Overflow (http://stackoverflow.com/questions/14171098/binding-datagrid-to-observablecollectiondictionary). The code is available at that link.
The code for the property in the view model is:
private ObservableCollection<BindableDynamicDictionary> granularrespondentstoreprobabilities = new ObservableCollection<BindableDynamicDictionary>();
public ObservableCollection<BindableDynamicDictionary> GranularRespondentStoreProbabilities
{
get
return (this.granularrespondentstoreprobabilities);
}
set
this.granularrespondentstoreprobabilities = value;
this.RaisePropertyChanged("GranularRespondentStoreProbabilities");
Thanks Walter, I was able to reproduce the issue now. Not really sure how to force the FieldLayouts to regenerate. The reason why it doesn't do it automatically is because the FieldLayout is tied to the data type BindableDynamicDictionary, so even if the data source instance changes it sees that the data type is still BindableDynamicDictionary so it tries to use the existing FieldLayout. However, clearing out the FieldLayouts before assigning the new data source instance doesn't force the grid to regenerate the FieldLayout.
I'm going to keep investigating so I will let you know what I find afterwards.
So I went back and tested this again and I was wrong about the grid not regenerating the field layout. The problem was that I was assigning a new data source instance and then adding data to it. Reassigning the instance immediately caused a property changed notification which updated the data grid but at that time there was no data in the collection so it generated an empty FieldLayout. What I needed to do was create the instance and store it in a local variable, add the items and then assign the local variable to the data source property so that when the grid updates all the data is ready.
So the key here is to call xamDataGrid1.FieldLayouts.Clear() before you change the data source. When your new data source is applied, this will trigger the FieldLayoutInitialized event and your code to create the columns can execute.