Select to view content in your preferred language

Having truoble in creating legend (TOC) in code behind.

2814
5
01-03-2011 01:06 AM
SanajyJadhav
Deactivated User
Hi,

I have to create a legend (new in 2.1) for our application to create dynamic TOC. I am having very hard time to do it in code behind.

I am referring a code sample at http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#LegendWithTemplates .Basically I want to replicate same TOC shown in this sample, but through code behind.

If any body has done this before in code behind, please guide me in tight direction.Sample code would of great help.

I would really appreciate any help as it has become very urgent issue for our application.

Thanks and regards,
Sanjay.
0 Kudos
5 Replies
DominiqueBroux
Esri Frequent Contributor
Code creating the same legend control than in the sample you pointed out:
public LegendWithTemplates()
{
    InitializeComponent();
    LayoutRoot.Children.Add(CreateLegend());
}
 
Legend CreateLegend()
{
    var legend = new Legend
    {
        LayerIDs = new[] { "Points of Interest", "United States" },
        LayerItemsMode = Legend.Mode.Tree,
        ShowOnlyVisibleLayers = false,
        Height = 300, Width = 200,
        HorizontalAlignment = HorizontalAlignment.Right,
        VerticalAlignment = VerticalAlignment.Top,
        Margin = new Thickness(20)
    };
 
    legend.SetBinding(Legend.MapProperty, new Binding {ElementName = "MyMap"});
    legend.Refreshed += Legend_Refreshed;
 
    legend.MapLayerTemplate = CreateTemplate(@"
       <StackPanel Orientation=""Horizontal"">
           <CheckBox Content=""{Binding Label}""
                   IsChecked=""{Binding IsEnabled, Mode=TwoWay}""
                   IsEnabled=""{Binding IsInScaleRange}"" >
           </CheckBox>
           <Slider Maximum=""1"" Value=""{Binding Layer.Opacity, Mode=TwoWay}"" Width=""50"" />
       </StackPanel>");
 
    legend.LayerTemplate = CreateTemplate(@"
       <CheckBox Content=""{Binding Label}""
           IsChecked=""{Binding IsEnabled, Mode=TwoWay}""
           IsEnabled=""{Binding IsInScaleRange}"" >
       </CheckBox>");
 
    return legend;
}
 
static DataTemplate CreateTemplate(string template)
{
    return (DataTemplate)System.Windows.Markup.XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"">" + template + "</DataTemplate>");
}
0 Kudos
dotMorten_esri
Esri Notable Contributor
Creating templates in codebehind is really bad practice. I suggest having the template in your app resources and assigning them. Ie. myToc.Template = Resources["layerTemplate"] as ControlTemplate;
0 Kudos
SanajyJadhav
Deactivated User
Dominique and Morten,

Thanks for your help and sample code snippet. ESRI guys have been amazing on this forum. You are really providing ton of help to the users through this forum.

I would try your code and put my result over here.

Thanks once again.
-
Sanjay.
0 Kudos
IkaRasidina
Deactivated User
Code creating the same legend control than in the sample you pointed out:
public LegendWithTemplates()
{
    InitializeComponent();
    LayoutRoot.Children.Add(CreateLegend());
}
 
Legend CreateLegend()
{
    var legend = new Legend
    {
        LayerIDs = new[] { "Points of Interest", "United States" },
        LayerItemsMode = Legend.Mode.Tree,
        ShowOnlyVisibleLayers = false,
        Height = 300, Width = 200,
        HorizontalAlignment = HorizontalAlignment.Right,
        VerticalAlignment = VerticalAlignment.Top,
        Margin = new Thickness(20)
    };
 
    legend.SetBinding(Legend.MapProperty, new Binding {ElementName = "MyMap"});
    legend.Refreshed += Legend_Refreshed;
 
    legend.MapLayerTemplate = CreateTemplate(@"
       <StackPanel Orientation=""Horizontal"">
           <CheckBox Content=""{Binding Label}""
                   IsChecked=""{Binding IsEnabled, Mode=TwoWay}""
                   IsEnabled=""{Binding IsInScaleRange}"" >
           </CheckBox>
           <Slider Maximum=""1"" Value=""{Binding Layer.Opacity, Mode=TwoWay}"" Width=""50"" />
       </StackPanel>");
 
    legend.LayerTemplate = CreateTemplate(@"
       <CheckBox Content=""{Binding Label}""
           IsChecked=""{Binding IsEnabled, Mode=TwoWay}""
           IsEnabled=""{Binding IsInScaleRange}"" >
       </CheckBox>");
 
    return legend;
}
 
static DataTemplate CreateTemplate(string template)
{
    return (DataTemplate)System.Windows.Markup.XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"">" + template + "</DataTemplate>");
}


Dear Dominique ,

Your code work correctly to reduce opacity for each layer in my application. But, I have other problem, when I unchecked the checkbox, the layer will disappear. Can you help me to solve my problem? For your information, I have 3 ArcGISDynamicMapServiceLayer  and they are created in code behind. Thanks for your help.
0 Kudos
DominiqueBroux
Esri Frequent Contributor

Your code work correctly to reduce opacity for each layer in my application. But, I have other problem, when I unchecked the checkbox, the layer will disappear.


likely 'ShowOnlyVisibleLayers' is set to true (which is the default value), try by setting it to false.
0 Kudos