Select to view content in your preferred language

Adding Text to the legend

1622
9
Jump to solution
08-30-2012 12:49 PM
TanyaOwens
Frequent Contributor
I am using the showcase template in API 2.4 (SL4), and I would like to add some additional text to the legend besides just the layers. Is there a way to do this?

-Thanks!
0 Kudos
1 Solution

Accepted Solutions
DominiqueBroux
Esri Frequent Contributor
If your need is just to display one label above the legend, why not create a vertical stackpanel with 2 UI elements : the label and the legend?

View solution in original post

0 Kudos
9 Replies
DavideLimosani
Frequent Contributor
You can use the Refreshed event to modify your legend. Add  Refreshed="Legend_Refreshed" to your xaml and then try someting like this:

 
       private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)
        {
          

            if (e.LayerItem.LayerItems != null)
            {
                foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)
                {
                    if (layerItemVM.Label == "myLayer")
                   {
                       layerItemVM.Label =  layerItemVM.Label + " additional text";
                   }
                }      
            }
        }


I think it should work. I didn't try it. And I'm not sure it is the answer to your question, but it may be a starting point
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Alternatively to the modification of the label by code, you can also change the MapLayerTemplate and add UIElement binded to any LayerItemViewModel property.
One of the most useful property is the Layer property that allows accessing all layer properties. Example to display layer CopyrightText:

<TextBlock Text="{Binding Layer.CopyrightText} />


For more complex scenario, we can also populate the Tag property by code and include it in the template:

<TextBlock Text="{Binding Tag} />
0 Kudos
TanyaOwens
Frequent Contributor
I think I might have not made it very clear what I am looking for. I would like a textblock at the beginning of the legend like:
<TextBlock TextWrapping="Wrap" Text="Select the timeslots you would like to review."/> With the checkbox layers below.

My current legend is modified from the showcase template:

MainPage.xaml:
            <userControls:DraggableWindow IsOpen="True" 
                                          x:Name="MapLegendWindow" 
                                          Margin="0,27,17,225" Padding="0" 
                                          HorizontalContentAlignment="Stretch" 
                                          VerticalContentAlignment="Stretch" 
                                          Title="Map Legend" Background="{StaticResource BaseColor}" 
                                          HorizontalAlignment="Right" Width="205">
                <i:Interaction.Triggers>
                    <i:EventTrigger>
                        <!--<actions:ToggleWindowVisibilityAction />-->
                        <!-- Hide at startup -->
                    </i:EventTrigger>
                </i:Interaction.Triggers>

                <esri:Legend x:Name="Legend" Map="{Binding ElementName=Map}" 
                             LayerIDs="AllAvailableFeatureLayer, AMAvailableLayer, PMAvailableLayer, NoneAvailableLayer"
                             LayerItemsMode="Flat"
                             ShowOnlyVisibleLayers="False"
                             Refreshed="Legend_Refreshed"/>
            </userControls:DraggableWindow>


MainPage.xaml.cs:
        private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)
        {
            LayerItemViewModel removeLayerItemVM = null;

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


                }

                if (removeLayerItemVM != null)
                    e.LayerItem.LayerItems.Remove(removeLayerItemVM);
            }
            else
            {
                e.LayerItem.IsExpanded = true;
            }

            {
                if (e.LayerItem.Label == "AllAvailableFeatureLayer")
                    e.LayerItem.Label = "AM and PM Available";

                if (e.LayerItem.Label == "AMAvailableLayer")
                    e.LayerItem.Label = "Only AM Available";

                if (e.LayerItem.Label == "PMAvailableLayer")
                    e.LayerItem.Label = "Only PM Available";

                if (e.LayerItem.Label == "NoneAvailableLayer")
                    e.LayerItem.Label = "No Times Available";
            }
        }


App.xaml:
        <Style TargetType="esri:Legend">
            <Setter Property="LayerItemsMode" Value="Flat" />
            <Setter Property="LayerTemplate">
                <Setter.Value>
                    <DataTemplate>
                        
                        <StackPanel Orientation="Horizontal">
                            
                            <CheckBox Content="{Binding Label}"
                          IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                          IsEnabled="{Binding IsInScaleRange}" >
                            </CheckBox>

                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="MapLayerTemplate">
                <Setter.Value>

                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox Content="{Binding Label}"
                          IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                          IsEnabled="{Binding IsInScaleRange}" >
                            </CheckBox>

                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>

            </Setter>
        </Style>
0 Kudos
TanyaOwens
Frequent Contributor
Alternatively to the modification of the label by code, you can also change the MapLayerTemplate and add UIElement binded to any LayerItemViewModel property.
One of the most useful property is the Layer property that allows accessing all layer properties. Example to display layer CopyrightText:

<TextBlock Text="{Binding Layer.CopyrightText} />


For more complex scenario, we can also populate the Tag property by code and include it in the template:

<TextBlock Text="{Binding Tag} />


If this allows me to add text to the to the beginning of the legend it is clear. When I add textboxes to my current code (see my post # 4 for more info) it doesn't show up.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
When I add textboxes to my current code (see my post # 4 for more info) it doesn't show up.


Sorry I don't see where you added textboxes in your code post#4.

But I you add textboxes in the datatemplate of the LayerTemplate and/or of the MapLayerTemplate, you should for sure see them in the legend next to the checkbox.
0 Kudos
TanyaOwens
Frequent Contributor

But I you add textboxes in the datatemplate of the LayerTemplate and/or of the MapLayerTemplate, you should for sure see them in the legend next to the checkbox.


That is one problem - I want to have the text at the beginning of the legend inside the Draggable Window:

Please select which time slot you would like to display:

Layer one (AM Available)
Layer two (PM Available
Layer three (Weekend Available)

In other words I am using the beginning text to explain how the legend can turn on and off the layers.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
If your need is just to display one label above the legend, why not create a vertical stackpanel with 2 UI elements : the label and the legend?
0 Kudos
TanyaOwens
Frequent Contributor
I am confused on how to do this with the draggable window...

            <userControls:DraggableWindow IsOpen="True" 
                                          x:Name="MapLegendWindow" 
                                          Margin="0,27,17,225" Padding="0" 
                                          HorizontalContentAlignment="Stretch" 
                                          VerticalContentAlignment="Stretch" 
                                          Title="Map Legend" Background="{StaticResource BaseColor}" 
                                          HorizontalAlignment="Right" Width="305">
                <i:Interaction.Triggers>
                    <i:EventTrigger>
                        <!--<actions:ToggleWindowVisibilityAction />-->
                        <!-- Hide at startup -->
                    </i:EventTrigger>
                </i:Interaction.Triggers>

                <esri:Legend x:Name="Legend" Map="{Binding ElementName=Map}" 
                             LayerIDs="Labels, AllAvailableFeatureLayer, AMAvailableLayer, PMAvailableLayer, WkEndAvailableLayer, CurrentlyUnAvailableLayer, NoneAvailableLayer"
                             LayerItemsMode="Flat"
                             ShowOnlyVisibleLayers="False"
                             Refreshed="Legend_Refreshed"/>
            </userControls:DraggableWindow>
0 Kudos
TanyaOwens
Frequent Contributor
Nevermind, I got it: The stackpanal looks a bit different that the draggable window background but it will work I think.

                <StackPanel Background="LightGray">

                    <TextBlock TextWrapping="Wrap" Text="Select the timeslots you would like to review."/>

                    <esri:Legend x:Name="Legend" Map="{Binding ElementName=Map}" 
                             LayerIDs="Labels, AllAvailableFeatureLayer, AMAvailableLayer, PMAvailableLayer, WkEndAvailableLayer, CurrentlyUnAvailableLayer, NoneAvailableLayer"
                             LayerItemsMode="Flat"
                             ShowOnlyVisibleLayers="False"
                             Refreshed="Legend_Refreshed"/>

                </StackPanel>


Now if you could help me with this http://forums.arcgis.com/threads/66163-Feature-DataForm-Need-Help I would greatly appreciate it.
0 Kudos