views:

249

answers:

2

I have a Silverlight app with a DataGrid containing some custom columns and all was working well. Then I updated to Silverlight 3 tools for VS 2008 SP1 and rebuilt it. Now it has the following problems:

  1. Rows aren't added when the collection is modified. The ItemsSource property is (and always has been) set to an ObservableCollection instance, which notifies when its contents change. This worked fine for Silverlight 2. However, in Silverlight 3 to get this working at all, I now have to null and then re-set ItemsSource - this seems like I'm hiding a bigger issue but I can't work out what that might be.

  2. I cannot select a row or a cell anymore. If I'm lucky, I can select one whole row before it stops working.

  3. I can't edit anything. I suspect this is related to the previous point.

I'll post some source when I am able, but first I have to strip it down to the bare minimum. In the meantime, I was hoping someone might have some idea of what may be going on here. My gut feeling on the second two points is that my bindings are no longer working, but that's just a guess and if it is the case, I have no idea which ones.

Thanks for any help anyone might be able to provide.

Update
So, I finally reduced my problem down to a simple works/doesn't work comparison. The problem seems to occur if I override Equals in my element type. As soon as I do that, something happens strangely in the ObservableCollection that contains that type, it seems, and my application breaks. To make it more interesting, there is a check to make sure that duplicate items don't even get close to being added to the collection. I don't exactly know why ObservableCollection needs to compare equality when inserting items (the stack trace indicates it is using IndexAt) but this seems to cause the issue.

So, any thoughts?

+1  A: 

I had the exact same issue on some of my code. It was wortking well when I was doing

ObservableCollection<MyType> typedObservableCollection = stuff;
myDataGrid.ItemsSource = typedObservableCollection;

but not when I was doing

ObservableObjectCollection observableCollection = stuff;
myDataGrid.ItemsSource = observableCollection;

I guess they changed the binding :( Type your ObservableCollection and that may work.

R4cOON
Thanks but my collection is already typed. +1 for the helpful info though.
Jeff Yates
Could you post your code? My datagrids are still working fine with SL3.
R4cOON
@R4cOON: I can't post the code, unfortunately. I am pretty sure that my problem relates to the complex databinding so I intend to refactor it. As soon as I have a simplified example of what doesn't work, I'll update this and see if you can identify the issue. Thanks so much for taking time to look at this - I really appreciate it.
Jeff Yates
A: 

I finally tracked down the initial problem that resulted in the symptoms I describe. I'm ashamed to admit it, but it was the simple error of a missing return keyword. My Equals override always returned false and under SL3, this caused a few problems (SL2 just didn't care, it seems).

So, that problem is solved. Let that be a lesson to everyone, if you override Equals in a reference type (or any type), make sure it actually works. Unit tests would've caught this. Shame on me.

Jeff Yates