0

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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Unknown Coder
  • 6,625
  • 20
  • 79
  • 129
  • I'm still looking for an answer to this one. Again, nothing in the class or the UI itself has changed. My VS solution had one project with both the UI and class. Now I have two projects (the original WPF and the new class library), same code in the class, but the class is now in the class library project. The end result simply does not make sense. – Unknown Coder Jun 04 '10 at 00:00

4 Answers4

4

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: 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.

Community
  • 1
  • 1
Unknown Coder
  • 6,625
  • 20
  • 79
  • 129
0

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
  • 48,890
  • 37
  • 186
  • 278
  • 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 – Unknown Coder May 24 '10 at 20:02
  • 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 May 25 '10 at 04:26
  • 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. – Unknown Coder Jun 03 '10 at 23:56
0

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
  • 3,152
  • 2
  • 27
  • 41
  • 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. – Unknown Coder May 24 '10 at 20:57
0

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;
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
bitbonk
  • 48,890
  • 37
  • 186
  • 278
  • 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. – Unknown Coder Jun 04 '10 at 15:55
  • I'm giving you the points because of your consistent help. Thanks. – Unknown Coder Jun 04 '10 at 16:21