Reputation: 75
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?
Upvotes: 1
Views: 210
Reputation: 75
The is one of those situations where my tools got in the way.
I have a base class that implements INotifyPropertyChanged server side. My service returns a collection of objects which are a subclass of it. Since its a subclass, my client needs access to the base-class. To solve this problem, I Add as Link a file client-side which really exists server-side. That way, when I update service references, a partial class is generated client-side (in references.cs) which looks a bit like this:
public partial class DataPointSubclass: Application.Server.Data.SharedCode.DataPoint, INotifyPropertyChanged { }
Its a pretty cool feature, except that it implements INotifyPropertyChanged, even though DataPoint also implements it. This is a big no-no, and was the root of all my problems. When you re-implement INPC on a subclass (at least, without overwriting the base class' property), PropertyChanged will always be null in the base class. In this case, the service reference automatically implemented INotifyPropertyChanged without checking if its base class already implemented it.
Solution: You have to disable this feature in References.svcmap. Look for this property and set it to false:
<EnableDataBinding>true</EnableDataBinding>
You can find References.svcmap by showing all files in solution explorer, and looking in your server references tree.
By doing this, classes automatically generated by updating your services references wont implement INotifyPropertyChanged automatically. If you want to have that functionality, you would presumably have to implement it manually.
Upvotes: 0
Reputation: 93601
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.
Upvotes: 1