Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
335
Best way to bind to a list of immutable structures with editing
posted

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 ?

Parents
  • 28407
    posted

    HI,

     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

     Sincerely,
     Matt
     Developer Support Engineer

     

     

    WpfApplication48.zip
Reply Children