The DXPropertyGrid supports adding new objects to collections using the collection editor. To pre-set certain properties for a new object or create an object without the default constructor, use the CollectionDefinition.NewItemInitializer property. Like PropertyDefinitionBase.InstanceInitializer, it should be set to an object that implements the DevExpress.Mvvm.Native.IInstanceInitializer interface.
Question Comments
Added By: Andrew Roberts 5 at: 11/5/2014 3:02:37 PM
I am trying to use this example but the ItemInitializer class needs to be in a ViewModel and the dxprg:CollectionDefinition is part of a DataTemplate in a separate file. I can't use NewItemInitializer as suggested in this sample.
Tried:
NewItemInitializer="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=DataContext.ItemInitializer}"
Received error:
System.Windows.Data Error: 40 : BindingExpression path error: 'ItemInitializer' property not found on 'object' ''WorkstationsEditorViewModel' (HashCode=43595579)'. BindingExpression:Path=DataContext.ItemInitializer; DataItem='WorkstationsEditorView' (Name='workstationsEditorView'); target element is 'CollectionDefinition' (Name=''); target property is 'NewItemInitializer' (type 'IInstanceInitializer')
So it’s looking for a property on WorkstationsEditorViewModel
So I did this on the ViewModel:
public ItemInitializer ItemInitializer {
get { return new ItemInitializer();}
Now not throwing errors, but I'm not getting the Collection property showing in the Property Grid
Added By: Andrey Marten (DevExpress Support) at: 11/5/2014 11:41:05 PM
Hello,
To process your recent post more efficiently, I created a separate ticket on your behalf: T169706: Example E4855 - Moving ItemInitialized to the View Model raises a binding exception. This ticket is currently in our processing queue. Our team will address it as soon as we have any updates.
Thanks,
Andrey