Thursday, August 16, 2012

Silverlight - Binding issue when using inheritence

Silverlight - Binding issue when using inheritence

I'm using a map from Esri, which offers a feature where you can bind a collection to the map, and it does all the magic for plotting and rendering graphic points. It just requires that the object has some properties to bind to, as shown below:

<esri:PointDataSource          ItemsSource="{Binding Path=PlottedData, Source={StaticResource ViewModel}, Mode=TwoWay}"          XCoordinateBinding="{Binding X}"          YCoordinateBinding="{Binding Y}"          IsSelectedBinding="{Binding IsSelected, Mode=TwoWay}"> 

My application, of course, requires more data than just those points, and there are different types too. This lead us to make subclasses of DataPoint for all of the different types, and then pass them through such that the map wouldn't be the wiser.

I had this working wonderfully in a demo application without the subclasses, but when I incorporate this into the application, and start receiving data from the services as a subclass, the IsSelected functionality stops working. I can confirm that the IsSelected properties on all of the points is being toggled when expected, but when i toggle them, they aren't reflected back onto the map. To clarify, if I replace the service results with this AFTER they're returned to the model:

var returned = new ObservableCollection<DataPoint>()     {         new DataPoint(){IsPlottable = true, IsSelected = false, X = 722762, Y = 488253},         new DataPoint(){IsSelected = false, X = 810000, Y = 550000, IsPlottable = true},         new DataPoint(){IsSelected = false, X = 801000, Y = 480000, IsPlottable = true},         new DataPoint(){IsSelected = false, X = 800100, Y = 500000, IsPlottable = true},         new DataPoint(){IsSelected = false, X = 800010, Y = 506000, IsPlottable = true}     }; 

then it works. WhenI then replace that with this:

var returned = new ObservableCollection<DataPoint>()     {         new DataPointSubclass(){IsPlottable = true, IsSelected = false, X = 722762, Y = 488253},         new DataPointSubclass(){IsSelected = false, X = 810000, Y = 550000, IsPlottable = true},         new DataPointSubclass(){IsSelected = true, X = 801000, Y = 480000, IsPlottable = true},         new DataPointSubclass(){IsSelected = false, X = 800100, Y = 500000, IsPlottable = true},         new DataPointSubclass(){IsSelected = true, X = 800010, Y = 506000, IsPlottable = true}     }; 

it stops working. Meanwhile, the points are plotted correctly--so I know that the binding is working at least when the bound collection is created (notice how the subclass above has some IsSelected = true; that binds right initially too).

What about my inheritence implementation might be causing the binding from basically switch from one/two-way binding to one-time binding? Could I be misattributing the issue?

EDIT: Thanks to comments below, I've narrowed it down to PropertyChange event handler being null whenever IsSelected is set. For clarity, here is where I define the public member of IsSelected in the base class:

    [DataMember]     public bool IsSelected     {         get { return _IsSelected; }         set         {             if (_IsSelected != value)             {                 _IsSelected = value;                 if (PropertyChanged != null)                     PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));             }         }     } 

In my demo app, where everything works, and if I use the first set of dummy points above (which work), PropertyChanged is not null, and the setter raises the PropertyChanged event.

When using a DataPointSubclass, however, PropertyChanged is always null (but the setter is being triggered). What could I be missing here?

Answers & Comments...

Answer: 1

Your service code has ObservableCollection<DataPoint>() so I am guessing the return type of the method is IQueryable<DataPoint> or IEnumerable<DataPoint>.

By doing this it is only serializing the bits it knows about (i.e. DataPoint) and ignoring the other members.

You need to change the expected type to be the derived class for it to serialise correctly.

Answer by HiTech Magic for Silverlight - Binding issue when using inheritence




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog