Select to view content in your preferred language

Get SubLayerID upon clicking on layer's name

1037
4
06-24-2012 09:55 PM
MiriEshel
Esri Contributor
Hi,

I have a legend like this:

       <Style TargetType="esri:Legend">
            <Setter Property="LayerItemsMode" Value="Tree" />
            <Setter Property="ShowOnlyVisibleLayers" Value="False" />
            <Setter Property="LayerIDs" Value="USAID_Layers" />

            <!-- Legend.LayerTemplate with checkbox for sublayer visibility-->
            <Setter Property="LayerTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                             <StackPanel Orientation="Horizontal">
                                   <Grid VerticalAlignment="Center">
                                    <CheckBox IsChecked="{Binding IsEnabled, Mode=TwoWay}" />
                                    <CheckBox IsChecked="{Binding IsEnabled}" IsEnabled="{Binding IsVisible}" IsHitTestVisible="False" />
                                </Grid>
                                 <TextBlock Text="{Binding Label}" VerticalAlignment="Center" Margin="2,0,0,0" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" />
                            </StackPanel>
                        </StackPanel>

                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

       private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TextBlock textSender = (TextBlock)sender;
            string layerName = textSender.Text;
         }

Upon cliking on the Textblock, I can get the layer name. I also need the SubLayerID in the same click. How can I grab that?

Thanks a lot,
Miri
0 Kudos
4 Replies
JoeHershman
MVP Alum
One way would be to bind Tag to the SubLayerID


<TextBlock Text="{Binding Label}" VerticalAlignment="Center" Margin="2,0,0,0" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" Tag="{Binding SubLayerID}"/>

Thanks,
-Joe
0 Kudos
MiriEshel
Esri Contributor
Ho Joe,

Thanks. It works. Simple and elegant.

Miri
0 Kudos
DominiqueBroux
Esri Frequent Contributor
One way would be to bind Tag to the SubLayerID


Right.
Or get the layerId directly by code without any binding:

private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
TextBlock textSender = (TextBlock)sender;
string layerName = textSender.Text;

  var layerId = (textSender.DataContext as LayerItemViewModel).SubLayerId;
}
0 Kudos
MiriEshel
Esri Contributor
Dominique,

Thanks to you too. What is the best out of these two options?

Thanks,
Miri
0 Kudos