Select to view content in your preferred language

Legend grouplayer template

977
5
06-18-2011 04:35 AM
evgeniykoryagin
Emerging Contributor
i need to show map content (map layers) in tree mode, and each type of node (map, grouplayer, layer) must have its own icon.
right now i am using esri Legend control with defined MapLayerTemplate and LayerTemplate. so my grouplayers have the same icon as layers.
is there a way to set a template for grouplayer?
or maybe it is possible to use default silverlight TreeView control in this case?
0 Kudos
5 Replies
DominiqueBroux
Esri Frequent Contributor
The easier way is to use the LegendItemViewModel.ImageSource property.

Hookup an handler to event Legend.Refreshed and, by code,  set the ImageSource in this handler (depending on the type of item).
Change the MapLayerTemplates and LayerTemplates to display this ImageSource.
0 Kudos
evgeniykoryagin
Emerging Contributor
Can you help me with some code examples?
how to change MapLayerTemplates and LayerTemplates to display LayerItemViewModel's ImageSource?
Here are my templates:
<esri:Legend x:Name="mapLegend" Background="White" Width="Auto" 
                                        LayerItemsMode="Tree" ShowOnlyVisibleLayers="False" 
                                        Refreshed="Legend_Refreshed" HorizontalAlignment="Left">

                    <esri:Legend.MapLayerTemplate>                        
                        <DataTemplate>                            
                            <StackPanel Orientation="Horizontal" >
                                

                                <Image Source="/Neolant.TORIS.WebClient;component/Images/mapservice.png" Margin="0,0,2,0"
                                                   VerticalAlignment="Center" Width="20" Height="20"/>
<TextBlock Text="{Binding Label}" VerticalAlignment="Center" HorizontalAlignment="Center"/>

                            </StackPanel>
                        </DataTemplate>
                    </esri:Legend.MapLayerTemplate>


                    <esri:Legend.LayerTemplate>
                        <DataTemplate>                             
                            <StackPanel Orientation="Horizontal" >
                                <StackPanel.Resources>
                                    
                                <Image x:Name="imgLayerItem"
                                    Source="/Neolant.TORIS.WebClient;component/Images/layer.png" Margin="0,0,2,0"
                                                   VerticalAlignment="Center" Width="20" Height="20" />                                
                                <TextBlock Text="{Binding Label}" VerticalAlignment="Center" HorizontalAlignment="Center"
                                           Margin="0,0,0,0"/>
                                
                            </StackPanel>                            
                        </DataTemplate>                        
                    </esri:Legend.LayerTemplate>                            

                </esri:Legend>
0 Kudos
DominiqueBroux
Esri Frequent Contributor
In the LayerTemplate, instead of <Image x:Name="imgLayerItem"
Source="/Neolant.TORIS.WebClient;component/Images/layer.png" Margin="0,0,2,0"
VerticalAlignment="Center" Width="20" Height="20" />
use
<Image x:Name="imgLayerItem"
Source="{Binding ImageSource}" Margin="0,0,2,0"
VerticalAlignment="Center" Width="20" Height="20" />

Then you have to set the ImageSource by code: hook up an handler to Legend.Refreshed event.

private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)
{
    if (e.LayerItem.LayerItems == null) return;
 
    foreach (var layerItem in e.LayerItem.LayerItems)
    {
        if (layerItem.IsGroupLayer)
            layerItem.ImageSource = new BitmapImage(new Uri("/Neolant.TORIS.WebClient;component/Images/group.png", UriKind.Relative));
        else
            layerItem.ImageSource = new BitmapImage(new Uri("/Neolant.TORIS.WebClient;component/Images/layer.png", UriKind.Relative));
    }
    // Note you could also set the ImageSource of e.LayerItem itself (e.g. depending on the type of map service), and you would have to change the MapLayerTemplate as well
}


Note : if you have more than one possible level of group layers, you'll have to enhance the code to go down the legend tree hierarchy
0 Kudos
evgeniykoryagin
Emerging Contributor
just made it! everything works fine! thanks a lot!
Note : if you have more than one possible level of group layers, you'll have to enhance the code to go down the legend tree hierarchy
- Made recursive method for this!
0 Kudos
DominiqueBroux
Esri Frequent Contributor
just made it! everything works fine! thanks a lot!
- Made recursive method for this!


Recursive is the easiest option. I gave the 'Descendants' code in this thread http://forums.arcgis.com/threads/18999-How-do-I-get-the-selected-LegendItem
0 Kudos