private void MyLegend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e) { if (e.LayerItem.LayerItems != null) { foreach (var sublayerItem in e.LayerItem.LayerItems) { if (sublayerItem.LegendItems != null && sublayerItem.LegendItems.Any()) // if there is a legend item sublayerItem.ImageSource = sublayerItem.LegendItems.First().ImageSource; // set the image of the sublayer with the image of the first legenditem } } }
<ListBox ItemsSource="{Binding LayerItems[0].LayerItems, ElementName=MyLegend}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Source="{Binding ImageSource}"/> <TextBlock Text="{Binding Label}" VerticalAlignment="Center" Margin="2,0"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
You might consider to use the legend control to get the symbols representing the sublayers. The legend control provides between 0 and many symbols for a sublayer (depending on the renderer). So the first question is 'which symbol do you want to associate to a sublayer?'
If you decide to associate the first symbol and if you use a legend control in your application, you could initialize the sublayer symbol in the refreshed event:
private void MyLegend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e) { if (e.LayerItem.LayerItems != null) { foreach (var sublayerItem in e.LayerItem.LayerItems) { if (sublayerItem.LegendItems != null && sublayerItem.LegendItems.Any()) // if there is a legend item sublayerItem.ImageSource = sublayerItem.LegendItems.First().ImageSource; // set the image of the sublayer with the image of the first legenditem } } }
Then you can display a listbox having its ItemsSource binded to a layer item of the legend ([0] = first map layer in this sample):<ListBox ItemsSource="{Binding LayerItems[0].LayerItems, ElementName=MyLegend}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Source="{Binding ImageSource}"/> <TextBlock Text="{Binding Label}" VerticalAlignment="Center" Margin="2,0"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Likely there are many other ways to get this result, just my 2cts.
I dont have Legend control in ESRI.ArcGIS.Client.Toolkit
Legend control is new with ArcGIS SL 2.1. Which version are you working with?
Yes, the API is backwards compatible. If there were any breaking change, they will be noted in this section of our SDK http://help.arcgis.com/en/webapi/silverlight/help/index.html#/What_s_new_in_2_1/016600000025000000/
You might consider to use the legend control to get the symbols representing the sublayers. The legend control provides between 0 and many symbols for a sublayer (depending on the renderer). So the first question is 'which symbol do you want to associate to a sublayer?'
If you decide to associate the first symbol and if you use a legend control in your application, you could initialize the sublayer symbol in the refreshed event:
private void MyLegend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e) { if (e.LayerItem.LayerItems != null) { foreach (var sublayerItem in e.LayerItem.LayerItems) { if (sublayerItem.LegendItems != null && sublayerItem.LegendItems.Any()) // if there is a legend item sublayerItem.ImageSource = sublayerItem.LegendItems.First().ImageSource; // set the image of the sublayer with the image of the first legenditem } } }
Then you can display a listbox having its ItemsSource binded to a layer item of the legend ([0] = first map layer in this sample):<ListBox ItemsSource="{Binding LayerItems[0].LayerItems, ElementName=MyLegend}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Source="{Binding ImageSource}"/> <TextBlock Text="{Binding Label}" VerticalAlignment="Center" Margin="2,0"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Likely there are many other ways to get this result, just my 2cts.
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.
<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: