Select to view content in your preferred language

Adding check boxes to individual Legend Items to control a feature layer display

3868
3
01-19-2012 09:37 AM
HugoCardenas
Emerging Contributor
Is there a way to add a check box to each Legend Item to turn on/off a feature layer features?

I have a feature service displaying point features using a renderer.  The Legend displays three different Legend Items: a red maker [large leaks], a yellow marker [small leaks], and a blue marker [no leaks].

The Legend XAML does place a check box for the layer, but not for each "Legend Item Template":
<esri:Legend x:Name="mapLegend" Map="{Binding ElementName=MyMap}" 
  LayerItemsMode="Tree" 
  ShowOnlyVisibleLayers="False" 
  Refreshed="Legend_Refreshed">

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

  <esri:Legend.LayerTemplate>
     <DataTemplate>
       <CheckBox Content="{Binding Label}"
          IsChecked="{Binding IsEnabled, Mode=TwoWay}"
          IsEnabled="{Binding IsInScaleRange}" >
       </CheckBox>
     </DataTemplate>
  </esri:Legend.LayerTemplate>

/* THE CODE IS FINE UNTIL HERE: I get a nice legend with 3 different markers and their proper Label.  */

  <esri:Legend.LegendItemTemplate>
    <DataTemplate>
       <CheckBox>
                   
       /* HERE IS MY PROBLEM: It places the red marker 3 times, while ignoring the other markers */       

       </CheckBox>
    </DataTemplate>
   </esri:Legend.LegendItemTemplate>

 </esri:Legend>


If I get to add a check box next to each "LegendItemTemplate" then I can control display of my feature service layer from the legend items:

Display only small leaks --> Check mark the "yellow" marker check box.
Turn off small leaks --> Uncheck the "yellow" marker check box.
And so forth...

I could then retrieve those features specified by the check box event using code-behind; no problem here.

Has anyone done something similar?
Is this the proper way to achieve this?

Your input is much appreciated.

Hugo.
0 Kudos
3 Replies
DominiqueBroux
Esri Frequent Contributor
<esri:Legend.LegendItemTemplate>
<DataTemplate>
<CheckBox>

/* HERE IS MY PROBLEM: It places the red marker 3 times, while ignoring the other markers */

</CheckBox>
</DataTemplate>
</esri:Legend.LegendItemTemplate>


That should not place the red marker 3 times, but place 3 different legend items.

Now as the template don't display any information about the legend item, I am not sure how you deduce that the same marker is displayed 3 times?

Try with a LegendItemTemplate such as:
<esri:Legend.LegendItemTemplate>
  <DataTemplate>
    <StackPanel Orientation="Horizontal" Margin="0,-1">
      <CheckBox VerticalAlignment="Center" />
      <Image Source="{Binding ImageSource}" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="None" MinWidth="20" />
      <TextBlock Text="{Binding Label}" Margin="5,0,0,0" VerticalAlignment="Center" />
    </StackPanel>
  </DataTemplate>
</esri:Legend.LegendItemTemplate>


You should see 3 different markers.
0 Kudos
HugoCardenas
Emerging Contributor
Thanks Dominique!
It works well.  I am impressed.  The line that solved my problem is the following:
<Image Source="{Binding ImageSource}" ... />

I did not know how to bind the "Source" property to the correct icons/images.  "ImageSource" took care of it.

The API documentation for the LegendItemTemplate Property states:

Turning on/off individual LegendItems or changing their opacity is not possible on any Layer type. It is possible to get creative and perform TOC style user interactions at the Legend.LegendItemtemplate level by discovering the parent objects of individual LegendItems.


And this is precisely the approach I will take with the C# code: I will capture the event fired by the checkbox of each item; thus, turn on/off this feature service features on its own attributes.  The API documentation has good clues on how to achieve this, unless you are aware of any other approach.

Again, thanks a lot!
Hugo.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Thanks Dominique!
It works well. I am impressed.

Thanks. You are welcome.

And this is precisely the approach I will take with the C# code: I will capture the event fired by the checkbox of each item; thus, turn on/off this feature service features on its own attributes. The API documentation has good clues on how to achieve this, unless you are aware of any other approach.

This approach looks good. I think I already did a sample like that but I don't remember where :confused:.

Let me know if you run into an issue I'll try removing dust from old samples:).
0 Kudos