I’m using Silverlight 5 with MVVM.
I have a ViewModel that is a Singleton. The ViewModel exposes a PointCollection that I am using to draw a Polyline in one of my views.
If I try to draw the same Polyline in a second view, by databinding to the PointCollection again, I get a “value does not fall within the expected range” exception.
As far as I could find out (with my limited Silverlight knowledge), this is caused by the fact that PointCollections are not shareable.
Is there a workaround? How can I get a second Polyline drawn that is identical to the first? I want to databind two Polylines to one PointCollection at the same time.
Edit: I haven’t found a solution, but someone with the same problem here. According to Microsoft:
This MSDN page mentions that some objects are not shareable and will genereate a “value out of range” exception.
http://msdn.microsoft.com/en-us/library/system.windows.resourcedictionary(VS.95).aspxThe PointCollection page also mentions that it is not shareable.
http://msdn.microsoft.com/en-us/library/system.windows.media.pointcollection(VS.95).aspxCurrently, this is by design behavior. However, we are evaluating this to see whether we can either change the behavior or at least the exception text.
Have a look at this question: Why doesn't this data binding work?
And at this one too: 2nd time binding to PointCollection not being rendered
As you gave little details I am not quite sure what is going on but these posts might help out. If not, please post your code.
I did some testing and the best solution I can think of is this:
Bind one PolyLine.Points to Points and the other PolyLine.Points to PointsClone.
It is a bit ugly because it will break when you use vm.Points.Add(point) instead of vm.AddPoint(point). By applying proper encapsulation you might be able to solve that.