ThinkGeo.com    |     Blog    |     Wiki    |     Support

INotifyCollectionChanged implementation partly broken in GeoCollection?

I’m using a TreeView that’s bound to my Map’s Overlay + Layers. I noticed that when calling the Layers.MoveUp and Layers.MoveDown functions, the TreeView does not update properly. It “duplicates” the item in the TreeView. I tested it out with a normal ObservableCollection instead for my Overlays and Layers and it worked fine. So I think it could be something in GeoCollection not working properly.

Here’s my xaml for the TreeView:

<TreeView ItemsSource="{Binding OverlaysViewSource}" Name="LayersTreeView">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Layers}" >
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding Name}" />
            </StackPanel>
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView> 

Here’s the related viewmodel code:

/// <summary>
/// The map's overlays.
/// </summary>
public GeoCollection<Overlay> Overlays { get; set; }

/// <summary>
/// Filters out the overlays that match the names for user overlays
/// </summary>
public ICollectionView OverlaysViewSource
{
    get
    {
        var itemsToFilter = CollectionViewSource.GetDefaultView(Overlays);
        itemsToFilter.Filter = i => (i as Overlay).Name != "useroverlay1" &&
                                    (i as Overlay).Name != "useroverlay2" &&
                                    (i as Overlay).Name != "useroverlay3";

        return itemsToFilter;
    }
}

public LayersViewModel()
{
    Overlays = MyMap.Overlays; // populates the Overlays collection
}

I’m then calling these two functions to move layers up and down:

Overlays[overlayName]).Layers.MoveUp(layer.Name); 

Overlays[overlayName]).Layers.MoveDown(layer.Name);

Below is a .gif showing the behavior I’m seeing. The red arrow up button moves the selected layer object up (towards index 0), and the red arrow button down moves the selected layer object down (towards the last index):

Could a developer check the code in GeoCollection and see if it’s working probably?

Hi Dan,

Our GeoCollection is not inherited from ObservableCollection, it’s inherited from Collection and it contains a Dictionary, I think that’s why you bind it to UI don’t works.

I am not sure whether it’s design for some reason, I will double check that with our development team.

It looks the GeoCollection have an event named CollectionChanged, I think you can implement your target via it, for example you can build a model class and then update the items bind to the event of CollectionChanged.

Regards,

Ethan

Thanks Ethan. Any update on this from the developers? I wouldn’t think it would matter if it didn’t inherit from ObservableCollection since it is implementing oncollectionchanged and notifypropertychanged, but maybe I’m wrong. Being able to bind to a lot of the map’s properties and collections out of the box saves some time creating custom classes.

Update: It looks like removing and reinserting the layer properly reflects changes in the tree. This should be fine for now.

Hi Dan,

Thanks to let us know it, our developer had implemented it, please get latest version package today and let us know whether it works for you.

Regards,

Ethan

Yup it works great now. Thanks! Tell the developers thanks too :slight_smile:

Hi Dan,

Thanks for your update, I am glad to hear that works.

Any question please let us know.

Regards,

Ethan