Tuesday, September 23, 2008

[Silverlight, C#] Polyline - Adding Points Does not Seem to Update on UI

I have been working on a small little drawing editor, which would allow the user to draw lines by clicking and dragging. I was using a Polyline to do this, and with every mouse move [or in some cases clicks] I would add a new point to the Polyline Points collection.

This, however, did not seem to work for me. When I added a new point directly into the Points collection, I saw no change in the actual UI indicating that the point had been added.
Polyline myLine = new Polyline();
// Setup visual look of polyline.
myLine.Points.Add(e.getPosition(null));
Not until I removed the element and then re-added it to the parent again could I actually see the new point.

I didn't want to have to remove and re-add to the DOM each time, but I found that I could force the UI to update if I actually changed and then reassigned the PointCollection of the Polyline.
PointCollection ptColl = myLine.Points;
ptColl.Add(e.getPosition(null));
myLine.SetValue(Polyline.PointsProperty, ptColl);
Something about this just doesn't seem right, but maybe I am misunderstanding something key. I am uncertain if this is a bug in beta 2, however, this was my work around for the issue.


.