Select to view content in your preferred language

LegendOptions or LegendLayer

2689
2
09-14-2012 10:15 AM
DorothyMortenson
Deactivated User
Has anyone tried to adjust the LegendOptions or LegendLayers in the codebehind?

I have a custome map, but I don't want the legend to indicate the base map name or a couple of other layers.  Documentation is a little thin, so I was hoping someone may have a little sample they wouldn't mind sharing.

Dorothy
0 Kudos
2 Replies
TanyaOwens
Frequent Contributor
You can choose which layers to display using the layer id's in the legend code:
                <esri:Legend x:Name="Legend" Map="{Binding ElementName=MyMap}"
                             LayerIDs="Labels, Public Schools, Non-Public Schools, Attendance Areas"
                             LayerItemsMode="Flat"
                             ShowOnlyVisibleLayers="False"
                             Refreshed="Legend_Refreshed" />


I also found the sames at this website helpful:http://broux.dominique.free.fr/Silverlight/InteractiveSDK/Default.htm#LegendWrapPanel

The code behind modification is done in the Legend Refreshed:
        private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)
        {
            LayerItemViewModel removeLayerItemVM = null;

            if (e.LayerItem.Layer is ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)
                e.LayerItem.LayerItems = null;

            if (e.LayerItem.LayerItems != null)
            {
                foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)
                {
                    if (layerItemVM.IsExpanded)
                        layerItemVM.IsExpanded = false;


                }

                if (removeLayerItemVM != null)
                    e.LayerItem.LayerItems.Remove(removeLayerItemVM);
            }
            else
            {
                e.LayerItem.IsExpanded = true;
            }
        }
0 Kudos
DorothyMortenson
Deactivated User
Thank you for responding.

My appologies. I was refering to the new Printing feature in  ArcServer version 10.1.

Here is what I came up with, in case anyone needs it.
[HTML]
private void ExportMap_Click(object sender, RoutedEventArgs e)
        {
            if (printTask == null || printTask.IsBusy) return;


            // Define the LegendOptions. This will show specified Layers in the printed map's legend area.
            LegendOptions myLegendOptions = new LegendOptions();

            // Define a List<LegendLayer> objects to put in the LegendOptions.
            List<LegendLayer> myLegendLayers = new List<LegendLayer>();

            // Loop through all of the Layers in the Map Control.
            foreach (var myLayer in MyMap.Layers)
            {
                // Create a new LegendLayer object to put in the List<LegendLayer> collection.
                LegendLayer myLegendLayer = new LegendLayer();

                // Set the LegendLayer.LayerId to the Layer.ID to add it to the printed map's legend area.
                myLegendLayer.LayerId = myLayer.ID;

                if (myLayer.ID == "Water Rights" || myLayer.ID == "OWRD Layers")
                {
                    // Add a single LegendLayer into the List<LegendLayer> collection.
                    myLegendLayers.Add(myLegendLayer);
                }
            }
            // Set the LegendOptions.LegendLayer to the new constructed List<LegendLayer> objects.
            myLegendOptions.LegendLayers = myLegendLayers;
            

            PrintParameters printParameters = new PrintParameters(MyMap)
            {
                LayoutOptions = new LayoutOptions() { Title=MapTitle.Text, 
                    Copyright = "Map generated by Or Water Resources Department", LegendOptions = myLegendOptions},
                ExportOptions = new ExportOptions() { Dpi = 96, OutputSize = new Size(MyMap.ActualWidth, MyMap.ActualHeight) },
                LayoutTemplate = (string)LayoutTemplates.SelectedItem ?? string.Empty,
                Format = (string)Formats.SelectedItem,
            };

            printTask.ExecuteAsync(printParameters);
        }
[/HTML]
0 Kudos