Sorry I forgot to mention that if you have a hierarchy of group layers, you have also to adapt the C# code to take into account this hierarchy.
Here is the code which should work whatever the number of group levels:
void Legend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e) { SetLayerItemImageSource(e.LayerItem); } private void SetLayerItemImageSource(LayerItemViewModel layerItem) { if (layerItem.LegendItems != null && layerItem.LegendItems.Any()) // if there is a legend item layerItem.ImageSource = layerItem.LegendItems.First().ImageSource; // set the image of the sublayer with the image of the first legenditem // Call recursively SetLayerItemImageSource if (layerItem.LayerItems != null) { foreach(var sublayerItem in layerItem.LayerItems) SetLayerItemImageSource(sublayerItem); } }Note that instead of using your own listbox and a binding to a legend control, you can also retemplate the legend control to use a listbox.
For example with this template:
<esri:Legend Map="{Binding ElementName=MyMap}" LayerItemsMode="Flat" ShowOnlyVisibleLayers="False" LayerIDs="California" Refreshed="Legend_Refreshed"> <esri:Legend.Template> <ControlTemplate TargetType="esri:Legend"> <ListBox ItemsSource="{TemplateBinding LayerItemsSource}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding IsEnabled, Mode=TwoWay}" IsEnabled="{Binding IsInScaleRange}" VerticalAlignment="Center"/> <TextBlock Text="{Binding Label}" VerticalAlignment="Center"/> <Image Source="{Binding ImageSource}" Margin="3,0"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </ControlTemplate> </esri:Legend.Template> <esri:Legend>you get this result:
layerItem.ImageSource = layerItem.LegendItems.First().ImageSource; // set the image of the sublayer with the image of the first legenditem
Is a basic legend control (without any code, templating,..) working well with your service?.
If answer is yes, it means that the imagesource is well initialized by the service. Then, the issue might come from the C# code I provided to initialize the imagesource of the layeritems.
Try to set a breakpoint on this line:layerItem.ImageSource = layerItem.LegendItems.First().ImageSource; // set the image of the sublayer with the image of the first legenditem
and look at the imagesource initialized at this point.
Yes, it did work.
............. and on the successive recursive calls the items are null.
I can't understand how the legend control could work well (i.e. the control displays the legend items with label+image) and at the same time, by code, the LegendItems are always null (i.e. looks like no swatches are returned by the legend service).
Is your map service public or private? (If public and if you want, send me your code dbroux@esri.com, I will take a look.)
A reminder just in case... : for private services, the legend control needs ArcGIS server 10SP1 and can't work with previous versions.
So, the ArcGIS server we're running is 10. Does it strictly have to be 10SP1?
If so, do you think that's the problem?
Again, if so, any other way I can work this out other than with the Legend control?
Yes ArcGIS server has to be 10SP1. Previous version of ArcGIS server don't provide any legend informations.
For sure, it is.
The problem with private services previous to 10SP1 is that there is no way to get the swatches of the sublayers.
One option is you to write your legend service (using SOAP API), another option is you to upgrade your server to SP1.
When you say a private service, you mean one that can't be accessed by anyone outside of my compan'y network. correct?
Also, I'll need some time to find some people in my office to confirm these, but, did anything under ArcGIS server 10 SP1 (private service) ever display such legends for other platforms? Or is it the case that ESRI never really supported such a feature in any platforms for public services?