Select to view content in your preferred language

Saving space inside the legend control

2407
6
02-23-2011 04:32 AM
FredericGoulet
Emerging Contributor
Hi there,

I would like to pack more items in a legend control before having to scroll to see all the content.

When there is only one symbol for a layer, I would like to put that symbol to the left of the layer label, on the same line, instead of a having a new line created under for the symbol.

When there is more than a symbol for a layer, I would like to keep the current behavor, the layer name alone on a line with all the symbols under on their own line.

I found this exemple of a legend template on this forum as a starting point but I have been unable to figure out what to do from there to archieve what I want.

<Grid.Resources>
      <Style x:Key="ItemsControl" TargetType="esri:Legend">
        <Setter Property="LayerItemsMode" Value="Flat" />
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="esri:Legend">
              <ScrollViewer Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"  
                        Padding="{TemplateBinding Padding}"
                        ScrollViewer.VerticalScrollBarVisibility="Auto"
                        ScrollViewer.HorizontalScrollBarVisibility="Auto"   
                            >
                <ItemsControl ItemsSource="{TemplateBinding LayerItemsSource}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}">
                  <ItemsControl.ItemTemplate>
                    <DataTemplate>
                      <StackPanel Orientation="Vertical">

                        <!--Layer Item-->
                        <ContentPresenter Content="{Binding}" ContentTemplate="{Binding Template}" Margin="0,2,0,2" />

                        <!--Legend Items-->
                        <ItemsControl ItemsSource="{Binding LegendItems}" >
                          <ItemsControl.ItemTemplate>
                            <DataTemplate >
                              <ContentPresenter Content="{Binding}" ContentTemplate="{Binding Template}" Margin="0,1,0,1" />
                            </DataTemplate>
                          </ItemsControl.ItemTemplate>
                        </ItemsControl>
                      </StackPanel>
                    </DataTemplate>
                  </ItemsControl.ItemTemplate>
                  <ItemsPresenter/>
                </ItemsControl>
              </ScrollViewer>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </Grid.Resources>

Does anybody has aready done that or can point me in the right direction ?

Thanks a lot

Frederic
0 Kudos
6 Replies
JenniferNery
Esri Regular Contributor
You can look at the following SDK sample if you need to change the DataTemplate of your Legend control: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#LegendWithTemplates
0 Kudos
FredericGoulet
Emerging Contributor
You can look at the following SDK sample if you need to change the DataTemplate of your Legend control: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#LegendWithTemplates


Hi Jennifer,

I know how to change the datatemplate of the legend control, but what I do not know is what to put in that datatemplate to do what I have describe in the message.
0 Kudos
JenniferNery
Esri Regular Contributor
When you are updating DataTemplate of any control, the template applies for all entries.

There is no special case handling that will do as you have described where a different template is used for layers with only one symbol and another template for more than one symbols. You will find that in the SDK sample, that each layer regardless of the symbol count follow the same DataTemplate.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
One option is that you use the event refreshed to associate an image to the layeritems and/or maplayeritems and to remove the lagenditems when there is only one entry.

Then you will have to retemplate the MapLayerTemplate and/or the LayerTemplate in order to include the image.

In this thread , there is a sample on how to associate an image to a LayerItem (coming from the first legend item), then you can delete the legenditems in order to save space.
0 Kudos
FredericGoulet
Emerging Contributor
It works great!

Thanks a lot.

Frédéric
0 Kudos
DominiqueBroux
Esri Frequent Contributor
It works great!


Bonne nouvelle:)
0 Kudos