Ah, very cool, great tip... that should make my code a little more efficent. Thanks Dom!In the hopes to maybe help someone else out, here is what I did:I have a wrapped template control around the ESRI legend control and it has its own LegendRefreshed which can be used by its consuming application. That consuming application can then subsicribe to this event and do as Dom said to get the Service layer and then call this method:
public void SetCustomLegend(LayerItemViewModel serviceLayerItemViewModel, String layerID, ObservableCollection<LegendItemViewModel> legendItems)
{
LayerItemViewModel layerItemViewModel = GetLegendLayer(serviceLayerItemViewModel, layerID);
if (layerItemViewModel != null)
layerItemViewModel.LegendItems = legendItems;
}
GetLegendLayer() is a part recursive, part iterative method to then get a sub-layer of the Map Layer (Service), because "e.LayerItem.Layer.ID" seems to only trigger at the Map Layer level.
protected LayerItemViewModel GetLegendLayer(LayerItemViewModel parentLayer, String layerID)
{
if(parentLayer.LayerItems != null)
{
for (Int32 i = 0; i < parentLayer.LayerItems.Count; i++)
{
//If match, return it
if (parentLayer.LayerItems.Label == layerID)
return parentLayer.LayerItems;
//Check sub layers
LayerItemViewModel layerItemViewModel = GetLegendLayer(parentLayer.LayerItems, layerID);
if (layerItemViewModel != null)
return layerItemViewModel;
//Continue checking other layers
}
}
return null;
}
Maybe there is a better way to do this... but it is working for me currently. Havn't tested it much yet though.Thanks again!