1

Can I select a layer, by code, in the TOC/legend of a map in a WPF ArcGIS Runtime application?

I develope in ArcGIS Runtime SDK 10.2.2 for .Net in C#.

1

Solved.

There is a property named IsSelected in ESRI.ArcGIS.Client.Toolkit.Primitives.LegendItemViewModel class, so I had to find my layer by service and sublayerId and set this property to true.

            ESRI.ArcGIS.Client.Toolkit.Primitives.LegendItemViewModel layer = ControlObjMap.Control.Object.LegendExtension.BusquedaItemSublayerId(leyenda.LayerItems, layerService, layerIndex);
            if (layer != null)
                layer.IsSelected = true;


    public static ESRI.ArcGIS.Client.Toolkit.Primitives.LegendItemViewModel BusquedaItemSublayerId(
        this IEnumerable<ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel> layerItems,string serviceName, int sublayerId)
    {
        if (layerItems == null)
            return null;

        ESRI.ArcGIS.Client.Toolkit.Primitives.LegendItemViewModel layer = null;

        foreach (var layerItem in layerItems)
        {
            if (layerItem.LayerItems != null)
            {
                layer = layerItem.LayerItems.BusquedaItemSublayerId(serviceName, sublayerId);
            }
            else if ((layerItem.Layer is ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer || layerItem.Layer is ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer) && 
                layerItem.SubLayerID == sublayerId)
            {
                if ((layerItem.Layer is ESRI.ArcGIS.Client.DynamicMapServiceLayer && ((ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)layerItem.Layer).Url == serviceName)||
                    (layerItem.Layer is ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer && ((ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)layerItem.Layer).Url == serviceName)
                    )
                    layer = layerItem;
            }

            if (layer != null)
                return layer;
        }

        return null;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.