What's the easiest/recommended way to bind to list of immutable structures ?
I have an ObservableDictionary class that happens to implement IList<KeyValuePair<K,V>> and its non-generic counterpart and related interfaces.
Unfortunately when bound to XamDataGrid, displayed items are not editable, since there are no setters.
To workaround this issue I have hidden every field using Visibility=Collapsed and duplicated each as UnboundField that is actually bound to relevant model property. To get proper editors I copy Field.DataType of each Field to matching UnboundField in FieldLayoutInitialized. This is enough to get editable editors but changes do not propagate to datasource.
To fix that final obstacle I had to handle EditModeEnding and EditModeEnded inside which I use e.Cell.Field and e.Cell.Record.DataItem to reflectively recreate new instance of row's DataItem (assuming that my immutable structure's constructor has an argument for each field of the same name and type) then finally replace the row with:
((IList)e.Cell.Record.DataPresenter.DataSource) [e.Cell.Record.DataItemIndex] = newEntry;
The question is: is there a better way of doing it and can I somehow make it reusable and/or global such that XamDataGrid will automatically use above approach whenever bound to a collection of immutable values ?
I've decided to keep using my solution because I don't want to change all existing viewmodels and I'd rather keep IDictionary<,> interface (which implies ICollection<KeyValuePair<,>>)
HI,
I am just following up on this post.
Please let me know if you need further assistance.
Sincerely, Matt Developer Support Engineer
In order to simply your editing, you could converter your KeyValuePair to an ojbect with a two properties and implement INotifyPropertyChanged.
I am attaching a sample project that convertes KeyValuePair to an object.
Please review my sample