/// <summary>
        /// Initializes a new instance of the <see cref="TableOfContents"/> class.
        /// </summary>
        public TableOfContents()
            : base()
        {
            DefaultStyleKey      = typeof(TableOfContents);
            _datasource          = new TocDataSource(this);
            ItemsSource          = _datasource;
            ItemTemplateSelector = new TocItemTemplateSelector(this);
#if !NETFX_CORE
            ContextMenuService.AddContextMenuOpeningHandler(this, ContextMenuEventHandler);
#endif
        }
        private void UpdateDatasource()
        {
            var tree = GetTemplateChild("TreeView") as ItemsControl;

            if (tree != null)
            {
                tree.ItemsSource = _datasource;
#if !NETFX_CORE
                ContextMenuService.AddContextMenuOpeningHandler(tree, ContextMenuEventHandler);
#endif
            }
        }
        /// <summary>
        /// Generates layer list for a set of <see cref="Layer"/>s in a <see cref="Map"/> or <see cref="Scene"/>
        /// contained in a <see cref="GeoView"/>
        /// </summary>
        internal void Refresh()
        {
            var list = GetTemplateChild("List") as ItemsControl;

            if (list == null)
            {
                return;
            }
#if !NETFX_CORE
            ContextMenuService.AddContextMenuOpeningHandler(list, ContextMenuEventHandler);
#endif

            if ((GeoView as MapView)?.Map == null && (GeoView as SceneView)?.Scene == null)
            {
                list.ItemsSource = null;
                return;
            }

            ObservableLayerContentList layers = null;
            if (GeoView is MapView)
            {
                layers = new ObservableLayerContentList(GeoView as MapView, ShowLegendInternal)
                {
                    ReverseOrder = !ReverseLayerOrder,
                };

                Binding b = new Binding();
                b.Source = GeoView;
                b.Path   = new PropertyPath(nameof(MapView.Map));
                b.Mode   = BindingMode.OneWay;
                SetBinding(DocumentProperty, b);
            }
            else if (GeoView is SceneView)
            {
                layers = new ObservableLayerContentList(GeoView as SceneView, ShowLegendInternal)
                {
                    ReverseOrder = !ReverseLayerOrder,
                };

                Binding b = new Binding();
                b.Source = GeoView;
                b.Path   = new PropertyPath(nameof(SceneView.Scene));
                b.Mode   = BindingMode.OneWay;
                SetBinding(DocumentProperty, b);
            }

            if (layers == null)
            {
                list.ItemsSource = null;
                return;
            }

            foreach (var l in layers)
            {
                if (!(l.LayerContent is Layer))
                {
                    continue;
                }

                var layer = l.LayerContent as Layer;
                if (layer.LoadStatus == LoadStatus.Loaded)
                {
                    l.UpdateLayerViewState(GeoView.GetLayerViewState(layer));
                }
            }

            ScaleChanged();
            SetLayerContentList(layers);
            list.ItemsSource = layers;
        }