I have a strange problem with Polyline class and it Points property. When I set the Points property in XAML explicitly it renders ok, later in code behind, for example I change the first point and it refresh the UI correctly.
<Polyline Name="plLine" Points="0 0 100 100 100 200 200 200"
Stroke="Black"
StrokeThickness="1" />
and later in code behind:
private void Button2_Click(object sender, RoutedEventArgs e)
{
plLine.Points[0] = new Point(10, 10);
}
everithing ok with that.
However when I bind Points property with the same data from my ViewModel, for instance:
<Polyline Name="plLine" Points="{Binding PointCollection}"
Stroke="Black"
StrokeThickness="1" />
and try to execute my code behind it does not refresh what is rendered.
Why this behavior?
You are
breaking the bindinghere. You have binded your points to property PointCollection and there after you are setting it from your code behind thereby overriding the binding. Afterwards, when you change the PointCollection in your ViewModel no change will be notice in your view even if you have implemented INotifyPropertyChanged for your class.You can try this small sample for this –
Since the binding has been broken by explicitly setting the Text property of your textbox.
So, you should always set your binded property via its source. In your case simply set the property PointCollection of your Viewmodel with INPC in place to see changes on view.
Edit:
Since its binded to PointCollection property that’s why changing the single value in collection won’t update it on UI. You have to completely remove the binding by setting it to new PointCollection Value. You can’t have both at same time. Look at this piece of code –
You need to set it to entirely to new collection. In case you want your binding not to be broken by setting directly as i mentioned above, uncomment my last line and comment the second last line. That way your binding will remain intact.