Select to view content in your preferred language

Remove root element of Legend

4455
13
07-12-2012 08:28 AM
by Anonymous User
Not applicable
Original User: miri

Hi everybody,

The legend issues never end....

I have two tabs: one for layers and one for legend.
Layer tab looks like this:

          <sdk:TabItem Header="Layers" Name="LayersTab" Margin="0,0,0,0">
                <Grid>
                    <esri:Legend LayerItemsMode="Tree" x:Name="MyLegend" Map="{Binding ElementName=Map}"
                                 LegendItemTemplate="{x:Null}" Refreshed="Legend_Refreshed"
                                 LayerIDs="USAID_Layers" ShowOnlyVisibleLayers="false">
                        <esri:Legend.LayerTemplate>
                            <DataTemplate>
                                <!--Layer Item-->
                                <StackPanel x:Name="checkBoxName" Orientation="Horizontal">
                                    <!-- Two overlapping checkboxes, the second checkbox is grayed when the layer is not visible-->
                                    <Grid VerticalAlignment="Center">
                                        <CheckBox IsChecked="{Binding IsEnabled, Mode=TwoWay}" />
                                        <CheckBox IsChecked="{Binding IsEnabled}" IsEnabled="{Binding IsVisible}" IsHitTestVisible="False" />
                                    </Grid>
                                     <TextBlock Text="{Binding Label}" Tag="{Binding SubLayerID}" VerticalAlignment="Center" Margin="2,0,0,0" MouseLeftButtonDown="ClickOnLayerName" />
                                </StackPanel>
                            </DataTemplate>

                        </esri:Legend.LayerTemplate>

                    </esri:Legend>

                </Grid>
            </sdk:TabItem>


Legend tab looks like this:
     
     <sdk:TabItem Header="Legend" Name="LegendTab">
                <Grid>
                    <esri:Legend Map="{Binding ElementName=Map}"
                         LayerItemsMode="Tree"
                        LayerIDs="USAID_Layers"
                        ShowOnlyVisibleLayers="True">
                    </esri:Legend>

                </Grid>
            </sdk:TabItem>


Enclosed with pictures to show how it looks like.
What I was asked is :
1. Remove the root element usaid_layers so it will show more data to the left
2. shrink the spaces from one layerItem to another in the legend tab.

Is that posiible? If yes, how?

Any help will be appreciated.

Thanks a lot,
Miri
0 Kudos
13 Replies
DominiqueBroux
Esri Frequent Contributor
What I was asked is :
1. Remove the root element usaid_layers so it will show more data to the left


Looks like you need to use LayerItemsMode="Flat" so the main layer will not show up.

Note : if for any reason this option is not ok for you and that you need something between 'Flat' and 'Tree', you'll nee to do it by code on event Legend.Refreshed.

2. shrink the spaces from one layerItem to another in the legend tab.


You can change the LegendItemTemplate (for example by using negative margins).
0 Kudos
by Anonymous User
Not applicable
Original User: miri

Hi Dominique,

As to issue #1, I thought to do it in Legend.Refreshed event but I didn't know how to catch the root element in order to remove it.
I need to remove it, not to collapse it.

As to issue #2, I will check it. Do you have a sample of changing LegendItemTemplate ? I looked at your wonderful site (http://broux.dominique.free.fr/Silverlight/InteractiveSDK/Default.htm#LegendTOC) but couldn't find one.

Thanks a lot,
Miri
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Hi Dominique,

As to issue #1, I thought to do it in Legend.Refreshed event but I didn't know how to catch the root element in order to remove it.
I need to remove it, not to collapse it.

As to issue #2, I will check it. Do you have a sample of changing LegendItemTemplate ? I looked at your wonderful site (http://broux.dominique.free.fr/Silverlight/InteractiveSDK/Default.htm#LegendTOC) but couldn't find one.

Thanks a lot,
Miri


Issue#1 : I still don't understand why 'Flat' mode is not working for you.

Issue#2 : Example of LegendItemTemplate skrinking the spaces between items and displaying image after the text:

<esri:Legend.LegendItemTemplate>
  <DataTemplate>
    <StackPanel Orientation="Horizontal" Margin="0,-3">
      <TextBlock Text="{Binding Label}" Margin="5,0" VerticalAlignment="Center" MinWidth="80" />
      <Image Source="{Binding ImageSource}" HorizontalAlignment="Center" VerticalAlignment="Center"  Stretch="None" MinWidth="20" Margin="0,-1" />
    </StackPanel>
  </DataTemplate>
</esri:Legend.LegendItemTemplate>
0 Kudos
by Anonymous User
Not applicable
Original User: miri

Hi Dominique,

Issue #1: Look at the attached image. I need the groups : USAID-related, Political/Administrative, Infrastructure, Israel-related to behave like TreeView (i.e. collapse/visible). Only the root element: USAID_Layers is redundant.

Issue #2: Perfect. Thanks.

Miri
0 Kudos
DominiqueBroux
Esri Frequent Contributor

Issue #1: Look at the attached image. I need the groups : USAID-related, Political/Administrative, Infrastructure, Israel-related to behave like TreeView (i.e. collapse/visible). Only the root element: USAID_Layers is redundant.

I understood that. But please try the Flat mode which looks to me a better starting point for your need.
You have just to change LayerItemsMode="Flat" in your XAML code.
0 Kudos
by Anonymous User
Not applicable
Original User: miri

Hi,

I tried already  LayerItemsMode="Flat" long ago but it gives me the attached image. It does not serve my needs since it doesn't show the groups.
How can I show the group layers in a flat mode?

Thanks,
Miri
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I eventually got it:)

Looks like you are in a case between rFlat' and 'Tree'.
After further investigations, my suggestion to do it by code on event Legend.Refreshed is not working because we can't remove the map layers from the legend (else the legend won't be updated when layer or sublayers visibility change).

The workaround might be to change the legend template and start the treeview hierarchy one level down.

<esri:Legend.Template>
    <ControlTemplate TargetType="esri:Legend"
                        xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
                        xmlns:sdk="clr-namespace:System.Windows;assembly=System.Windows.Controls">
        <toolkit:TreeView   ItemsSource="{Binding LayerItemsSource[0].LayerItemsSource, RelativeSource={RelativeSource TemplatedParent}}"
                            Background="{TemplateBinding Background}"
                            Foreground="{TemplateBinding Foreground}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                                  Padding="{TemplateBinding Padding}"
                            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"                >
            <toolkit:TreeView.ItemTemplate>
                <sdk:HierarchicalDataTemplate ItemsSource="{Binding LayerItemsSource}" >
                    <ContentPresenter Content="{Binding}" ContentTemplate="{Binding Template}" />
                </sdk:HierarchicalDataTemplate>
            </toolkit:TreeView.ItemTemplate>
            <toolkit:TreeView.ItemContainerStyle>
                <Style TargetType="toolkit:TreeViewItem">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                </Style>
            </toolkit:TreeView.ItemContainerStyle>
         </toolkit:TreeView>
    </ControlTemplate></esri:Legend.Template>


Note how is set the ItemsSource of the TreeView : ItemsSource="{Binding LayerItemsSource[0].LayerItemsSource, RelativeSource={RelativeSource TemplatedParent}}

Instead of starting the treeview hierarchy from LayerItemsSource, we start it from the sublayers of the first layer (that supposes there is only one layer in the legend but it's your case).

Hope this helps.
0 Kudos
by Anonymous User
Not applicable
Original User: miri

Hi Dominique,

You are genius! It works great.

Thanks a lot,
Miri
0 Kudos
MiriEshel
Esri Contributor
Hi,

After all this work of removing the top element, it looks like I won't use it since I need more layers there....Anyway:
1. I would like to add a featureLayer to my legend that looks like this:

                      <esri:Legend.LayerTemplate>
                            <DataTemplate>
                                <!--Layer Item-->
                                <StackPanel x:Name="checkBoxName" Orientation="Horizontal">
                                    <!-- Two overlapping checkboxes, the second checkbox is grayed when the layer is not visible-->
                                    <Grid VerticalAlignment="Center">
                                        <CheckBox IsChecked="{Binding IsEnabled, Mode=TwoWay}" />
                                        <CheckBox IsChecked="{Binding IsEnabled}" IsEnabled="{Binding IsVisible}" IsHitTestVisible="False" />
                                    </Grid>
                                    <!--<Image Source="{Binding ImageSource}" VerticalAlignment="Center" MinWidth="20" />-->
                                    <TextBlock Text="{Binding Label}" Tag="{Binding SubLayerID}" VerticalAlignment="Center" Margin="2,0,0,0" MouseLeftButtonDown="ClickOnLayerName" />
                                </StackPanel>
                            </DataTemplate>

                        </esri:Legend.LayerTemplate>

The feature layer is added but without the checkbox (only the name). Why?

2. My feature layer is a cluster layer. Its default legend looks like in the attached image.
   Is it possible to show more detailed legend, not only the FlareClusterer?

Thanks a lot as always,
Miri
0 Kudos