Why do I do this? Because looks like it only needed if I use Add New Row functionality, but I don't.
It appears that the documentation article that you have linked is also missing some information. It is not only the AddNewRow in the XamGrid that requires an empty constructor on your underlying data item. Filtering in the XamGrid also requires this, and so I believe this documentation article may need to be updated to reflect that.
Are you allowing filtering operations in your XamGrid? If so, I would recommend following the steps in the documentation article to use the DataObjectRequested event to return an item to be used with your filtering operations.
If you are not allowing filtering operations in the XamGrid and are still seeing this exception, then would it be possible for you to please provide the code that you are currently using for your XamGrid so I may investigate further and try to determine why you are seeing this exception?
Please let me know if you have any other questions or concerns on this matter.
Yes, I allow filtering, but I still don't understand why this object needed. Also, I use filtering as Filter menu and I don't see the similar approach with AddNewRow functionality. What does this object do in the internal logic of the XamGrid?
Internal to the XamGrid, if filtering or an AddNewRow is enabled, we use reflection to attempt to create an empty object of the type that corresponds to a particular ColumnLayout in the grid. For filtering, this is used as a sort of placeholder to filter against. For the AddNewRow, this is used as a placeholder "template" for an underlying data item to correspond to the new records that will be added to the grid, and as such, the underlying collection. It appears to me that much of this is likely done for performance reasons, as we wouldn't want to get the underlying type of the data items to the rows for each row filtered or added each time we go to do this operation and so the placeholder is used to prevent this unnecessary overhead.
If there is no default, parameter-less constructor for the elements to be created via reflection, we require that you create one and return it in the DataObjectRequested event. We cannot assume that we need to pass N number of parameters to the element we create, as this number could be any number of parameters. As such, I would recommend that you either return an "empty" object from the DataObjectRequested event as shown in the forum thread linked above, or add an empty, parameter-less constructor to your underlying data item, if possible.
OK got it, Thanks
The last question, why this object needed when I use the filter menu?
So does this mean that if you intend on using filtering in your xamGrid that the view model for the object must always have default values when called with the parameter less constructor?
For example, you couldn't do something like this, as doing so could result in null reference exception if the Infragistics code were to create an instance of the class calling the parameter less constructor and then tried to access it's property:
// Parameterless constructor to satisfy this requirement.
// Actual constructor that gets called throughout the code.
class SomeClass(object someObject)
_someObject = someObject;
// Property1, which will throw a null reference exception if accessed when the object is constructed from the parameter less constructor