MapItemsControl does not reflect removing of corresponding ItemsSource

171 views Asked by At

Within a UWP-App (Windows 10) I've got a MapControl and I'm using MapItemsControl to deliver an overlay for that map. The ItemsSource of that MapItemsControl (which is an ObservableCollection) is bound via xaml, but is only working in one direction:

Adding items to that collection is working fine and those items are shown in that MapControl too. Removing items to that collection is working too, but seemingly only within that collection - the visual representation on my MapControl does not react to removing elements. This can lead to infinite adding of items into that map, while no item gets ever removed.

The ObservableCollection gets updated quiet frequentely (via MapControl.ZoomLevelChanged-Event) and gets cleared & repopulated in that process - might that be a problem?

Binding via xaml looks like this:

 <maps:MapControl
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    [...]>
    <maps:MapItemsControl ItemsSource="{x:Bind Path=MapDirectionOverlay, Mode=OneWay}"/>
</maps:MapControl>

Any suggestions?

2

There are 2 answers

0
Jerome Dreyer On BEST ANSWER

Since using Clear-Method did not do the trick, I tried using other remove-methods of the ObservableCollection and eventually that worked.

So in the end, this is the workaround I'm using:

private new void Clear()
{
    for (int i = this.Count - 1; i >= 0; i--)
    {
        this.RemoveAt(i);
    }
}

After all I still don't get, why a simple Clear would not work, since it still should raise a NotifyCollectionChangedAction. (Correct me if I'm wrong)

1
Sunteen Wu On

Removing items to that collection is working too, but seemingly only within that collection- the visual representation on my MapControl does not react to removing elements.

ObservableCollection represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. So if you add or remove items, the collection and ItemsSouce of binding should be reflect updated.

Since I don't know how you removing the item, by using remove methods of ObeservableCollection such as Remove,RemoveAt and RemoveItem, they should work well with removing items as well as removing correspondent item in the map.

But if you just set the ObeservableCollection to null will take no effect on the ItemsSouce.In this scenario you need to set the ItemsSouce of MapItemsControl to null manually but which is not recommend.