Hello,
I have downloaded the Esri Toolkit for ArcGIS Runtime 100.2.1 Beta 3, and I was wondering if anyone could provide me with a way that I can access the selected layer of the table of contents control. I am using the ArcGIS runtime project template that includes the MainWindow.xaml and MapViewModel.cs class.
I have implemented the TOC control within the MainWindow, and it is automatically loading the layers that I have wired up within the MapViewModel.
<esri:TableOfContents Grid.Column="0" x:Name="toc" GeoView="{Binding ElementName=MainMapView}" ShowLegend="False" FontSize="18" Selector.SelectionChanged="toc_SelectionChanged" ></esri:TableOfContents>
<CheckBox Content="Show Legend" IsChecked="{Binding ShowLegend, ElementName=toc, Mode=TwoWay}" Margin="10,30,10,10" />
But how do I access the selected layer of the TOC control within the MapViewModel when I interact with the TOC during runtime?
Thank you!
Jen
You can update the TableOfContents LayerItemTemplate property and add an event say MouseUp on Layer name, update its Binding to retrieve LayerContent, this should match the layer on your map. From the original source code, I only modified this bit - line 60-61 (see Text and DataContext).
<Grid.Resources>
<HierarchicalDataTemplate x:Key="MyLayerTemplate" ItemsSource="{Binding Sublayers}">
<Grid Margin="0,2" >
<Grid.Resources>
<Style TargetType="MenuItem" x:Key="HideMenuItemWhenDisabled">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding IsInScaleRange, Mode=OneWay}" Value="false">
<Setter Property="Opacity" Value=".5" />
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Grid.ContextMenu>
<ContextMenu>
<MenuItem IsCheckable="True" IsChecked="{Binding LayerContent.IsVisible, Mode=TwoWay}" Header="Enabled" IsEnabled="{Binding LayerContent.CanChangeVisibility}" Style="{StaticResource HideMenuItemWhenDisabled}" />
<MenuItem Command="{Binding ZoomToCommand}" Header="Zoom to" Style="{StaticResource HideMenuItemWhenDisabled}" />
<MenuItem Command="{Binding ReloadCommand}" Header="Reload" Style="{StaticResource HideMenuItemWhenDisabled}" />
</ContextMenu>
</Grid.ContextMenu>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<esri:SymbolDisplay Symbol="{Binding Symbol, Mode=OneTime}" Margin="0,0,5,0" MaxHeight="40" MaxWidth="40">
<esri:SymbolDisplay.Style>
<Style TargetType="esri:SymbolDisplay">
<Style.Triggers>
<DataTrigger Binding="{Binding Symbol, Mode=OneTime}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</esri:SymbolDisplay.Style>
</esri:SymbolDisplay>
<CheckBox Grid.Column="1" IsChecked="{Binding LayerContent.IsVisible, Mode=TwoWay}">
<CheckBox.Style>
<Style TargetType="CheckBox">
<Style.Triggers>
<DataTrigger Binding="{Binding LayerContent.CanChangeVisibility, Mode=OneTime}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
<TextBlock Text="{Binding Name}" MouseUp="OnMouseUp"
DataContext="{Binding LayerContent, Mode=OneWay}" Grid.Column="2" VerticalAlignment="Center">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding HasError}" Value="True">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding IsActive}" Value="False">
<Setter Property="Opacity" Value=".5" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</HierarchicalDataTemplate>
</Grid.Resources>
<esri:TableOfContents Grid.Row="0" x:Name="toc"
LayerItemTemplate="{StaticResource MyLayerTemplate}"
GeoView="{Binding ElementName=mapView}" ShowLegend="False" >
private void OnMouseUp(object sender, MouseButtonEventArgs e)
{
var datacontext = (sender as FrameworkElement)?.DataContext as ILayerContent;
}
Thanks Jennifer!
Your example was just what I needed. I can now select each layer in the table of contents and subsequently query for the attributes with a tool I created.
Regards!
Jen