This is a migrated thread and some comments may be shown as answers.

AddNewItem() and zero-argument constructors

1 Answer 61 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 19 Dec 2012, 06:33 PM
I was having a problem, and was going to post asking for help, but I figured it out. And then thought I might post about the problem I ran into, and the solution, in case someone else runs into it.

I have a user control that contains a RadDataForm and a RadGridView, both of which have their ItemsSource properties bound to a QueryableCollectionView that is wrapped around an ObservableItemCollection.

And I have a "New" button that is bound to a command that calls RadDataForm.AddNewItem().

The objects in the collection were instances of a ORM record class, and the fields in the form and the the grid were bound to properties of the record class, and all was well and good.

Until I decided that I needed to add additional validation. I couldn't modify the record class - it is shared across several projects - so I did what I have often done, I defined a new wrapper class. The constructor for the new wrapper class took a an instance of the record class as an argument, and exposed it as a property (and also exposed the additional functionality I needed.)

So I modified the collection to contain instances of this wrapper class, and modified the bindings to properties of the record class property of the wrapper, instead of to properties of the record class itself.  And then ran the program to see how it would do.

The grid displayed fine, and the form viewed existing records fine. But when I clicked my "New" button, AddNewItem() returned false.

This confused me. I could see how this would happen, if ItemsSource was bound to a fixed-size array, but I should be able to insert new items into a ObservableItemCollection.

The problem turned out to be the constructor of the wrapper class. It seems the element type in the collection must have a zero-argument constructor. Once I added this, everything worked again.

1 Answer, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 20 Dec 2012, 07:43 AM
Hi Jeffrey,

Thank you for sharing your experience with the community.  

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
DataForm
Asked by
Jeff
Top achievements
Rank 1
Answers by
Maya
Telerik team
Share this question
or