My application has a line through a set of points, with an icon at each point. I have the following XAML within a Canvas element:
<!-- A route line -->
<Polyline Canvas.ZIndex="1" Stroke="Green" StrokeThickness="5" Points="{Binding SelectedRoute.Items, Converter={StaticResource routePointsConverter}}"></Polyline>
<!-- The icons on a route line -->
<ItemsControl Canvas.ZIndex="2" ItemsSource="{Binding SelectedRoute.Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding X, Converter={StaticResource canvasIconCenterConverter}}"/>
<Setter Property="Canvas.Top" Value="{Binding Y, Converter={StaticResource canvasIconCenterConverter}}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="Graphics\Icons\x.png" Width="20" Height="20"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
SelectedRoute.Items is an ObservableCollection. The problem is that the Polyline is not updated when and item is added to SelectedRoute.Items. The icon is added when an item is added to Items. The line is also correctly drawn if I change the bound Items of SelectedRoute to a different one and then back. This seems to be a problem specifically with Polyline.
When I debug I can see that the converter for the Polyline is not called when an item is added. Why is this happening?
Note, I've found a workaround but would like to understand why it isn't working in the solution shown.
WPF registers for INotifyCollectionChanged
updates only for the ItemsControl.ItemsSource
property. It actually has nothing to do with the Binding
extension. You can even directly assign (ie without Binding
) the ItemsSource
property to a collection that implements INotifyCollectionChanged
, and the items control would be notified of changes to that collection.
For the Points
binding to be updated, the Items
property must fire the PropertyChanged
event. You could fire it "manually" every time you change the collection.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.