locked
Binding ModelItemCollection to DataGrid ItemsSource (IEditableCollectionView not implemented by ModelItemCollection) RRS feed

  • Question

  • Hello,

    I'm binding a property that is a ModelItemCollection to the DataGrid Items source property without using any converter. In this scenario, the DataGrid doesn't add an empty row to permit the insersions of new items, so the only way I have to add new items is by adding them manualy using the ModelItemCollection property Add method.

    If on the other hand I bind the same property that is a ModelItemCollection to the DataGrid Items source property but this time using the converter ModelToObjectValueConverter, the grid now adds an empty row to permit the insersions of new items, but I loose all the notifications on the ModelItem (It means I don't know when items were added/removed or changed in the collection). I also suspect that this might not be the right way to change the items in this collection has it looks that I'm bypassing the ModelItem way of doing things.

    I think the problem described in the first sentence might be because the ModelItemCollection doesn't implement the IEditableCollectionView, which I think is the expected interface for the DataGrid to allow insertion of new items.

    Is there a way to turnaround this?

    Friday, July 23, 2010 7:36 PM

Answers

  • I suggest that you clone your collection and then bind the cloned collection to the data grid. If you take a look at  System.Activities.Presentation.TypeCollectionPropertyEditor in reflector you will see this is the approach taken. Allow the user to edit that collection and then if Dialog.OK clear the ModelItemCollection and manually add each edited collection item back into the ModelItemCollection. This supports undo/redo as you have been working via the ModelItemTree. If you dont clone your collection its also difficult to allow cancelling out of the edit.
    • Marked as answer by Nelson Morais Monday, July 26, 2010 4:15 PM
    Monday, July 26, 2010 6:14 AM

All replies

  • I suggest that you clone your collection and then bind the cloned collection to the data grid. If you take a look at  System.Activities.Presentation.TypeCollectionPropertyEditor in reflector you will see this is the approach taken. Allow the user to edit that collection and then if Dialog.OK clear the ModelItemCollection and manually add each edited collection item back into the ModelItemCollection. This supports undo/redo as you have been working via the ModelItemTree. If you dont clone your collection its also difficult to allow cancelling out of the edit.
    • Marked as answer by Nelson Morais Monday, July 26, 2010 4:15 PM
    Monday, July 26, 2010 6:14 AM
  • TRex,

    After trying several aproaches, I decided to stick with the ModelItemCollection and add a button to insert rows to the collection. I opt by this solution because the DataGrid I'm using is placed directly in the ActivityDesigner, so there's no dialog where I could intercept the Dialog.Ok to perform the update on the ModelItemCollection.

    However, if I've opted to have a dialog, I do agree with you that clone and later on resync the ModelItemCollection would be a solution.

    I'm using this on a SwitchList Activity to overcame a lack of functionality on the Switch activity where we can't use the same "Case Activity Block" on several "Cases". This SwitchList<T> activity allows me to do this.

    Thanks!

    Monday, July 26, 2010 11:29 AM