tags:

views:

82

answers:

4

I've been working with the datagrid in WPF with great results. However, it is now giving me unexpected results after some changes.

BEFORE: I had a datagrid on a page. The DataContext was set to a List object that was created from a class that existed within the same WPF project. The empty row at the bottom of the datagrid, to add new records, is visible

AFTER: Same page, same datagrid. But now the List object is coming from a Class Library project within the same solution. EXACT same code, but it's now been extracted into a class library. The empty row at the bottom of the datagrid, to add new records is not visible.

WTF?

A: 

What kind of list is it? Does its publically visible interface allow to add items or is it a readonly list now (e.g. IEnumerable, ICollection?

bitbonk
It just a System.Collection.Generic List, nothing special really. The list itself is composed of custom objects. The code itself has not changed it's merely gone from being in the WPF project to a class library project
Jim Beam
This shouldn't happen just by moving the class with the property you bound you ItemSource against to a different assembly, something else must have changed with it.
bitbonk
That's truly the strange part - nothing else has changed. I literally just dragged and dropped between the two projects in the solution, deleted it out of the WPF project and then changed the reference to the new class library and that's it. It just doesnt make sense.
Jim Beam
A: 

I encountered the same problem when I set the DataGrid property IsReadOnly="True". Check if you have the same setting and try to remove it to see what happens.

Maurizio Reginelli
Its a good suggestion and I tried it, but I get the same (unwanted) result. Again, nothing on the UI side has changed, the only difference is where the DataContext list object is coming from.
Jim Beam
A: 

Maybe it is some security issue or even a bug. I just read this:

I found that if you access the CanAddRow of ListCollectionView once before you use the collection, magically the CanUserAddRows of the DataGrid becomes true. Strange!

IEditableCollectionView ecv = new ListCollectionView(myRecordCache);
bool b = ecv.CanAddNew;   // dummy access
MyGrid.DataContext = ecv;
bitbonk
I understand what you're saying but I'm not setting the DataContext in any different way than when it was working. Again, the *only* thing that changed is the location of the class that produces the list of objects.
Jim Beam
I'm giving you the points because of your consistent help. Thanks.
Jim Beam
+4  A: 

I think I finally have the answer. Basically, I was mistaken, I did change a tiny portion of the class. The "lightbulb" went on when I read the answers to this one: http://stackoverflow.com/questions/480098/how-do-i-create-a-new-row-in-wpf-datagrid-when-it-is-bound-to-an-xmldataprovider

Bottom Line: The class you are binding to needs to have a default constructor in order to display an editable row!

In my code, I did change the constructors (I completely forgot about that) which left no default. Adding the default constructor back into the class fixed the problem.

Jim Beam