Select to view content in your preferred language

sublayer visibility through datatemplate

639
2
07-07-2010 12:02 PM
MikeTischler
Emerging Contributor
hi,
I'm dealing with a few different types of layers (WMSlayer and dynamicservicelayer) that are composed of several sublayers.  I've bound them to a treeview, so that each layer service is a parent node with child nodes for the sublayers.  I'd like to implement controls that toggle the visibility of the sublayers.  I've looked at the sublayer API sample online as a guide, and it works great - provided you only have 1 service, as the service name is hardcoded in the example. 

I'm having a difficult time trying to reference the parent service layer from within my nested datatemplate.  The hierarchicaldatetemplate needs to be nested (at least I think it does) because the same property (Name) is not valid for both parent and child layers of a service.  The sticking point is the childTemplate CheckBox Name...I'd like to be able to reference/bind the name of the parent service, but I can't figure out how...either in codebehind or in XAML binding.



<sdk:HierarchicalDataTemplate x:Key="childTemplate" ItemsSource="{Binding Layers}">
   <StackPanel Orientation="Horizontal">
              <!--Layer visibility checkbox-->
              <CheckBox Name="CurrentEvents" IsChecked="{Binding DefaultVisibility, Mode=TwoWay}" 
     VerticalAlignment="Center" Tag="{Binding ID}" ClickMode="Press" Click="CheckBox_Click" />
              <!--Layer name-->
              <TextBlock Text="{Binding Name, Mode=OneWay}" Width="75" Foreground="AntiqueWhite" Margin="5,0,0,0" 
     VerticalAlignment="Center" > 
              </TextBlock>
   </StackPanel>
  </sdk:HierarchicalDataTemplate>
  <sdk:HierarchicalDataTemplate x:Key="categoryTemplate1"  ItemsSource="{Binding Layers}" ItemTemplate="{StaticResource childTemplate}" >
      <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked="{Binding Visible, Mode=TwoWay}" VerticalAlignment="Center" />
                            <!--Opacity slider-->
                            <Slider Margin="-5,0,0,0" Minimum="0" Maximum="1" Width="30" 
                                Value="{Binding Opacity, Mode=TwoWay}" Height="18" />
                         <TextBlock Text="{Binding ID}" />
     </StackPanel>
  </sdk:HierarchicalDataTemplate>
0 Kudos
2 Replies
DominiqueBroux
Esri Frequent Contributor
One option is to use the datacontext of the layer TreeViewItem.
You have to get the sublayer treeview item and go up the hierarchy to get the layer treeview item.

 
private TreeViewItem GetTreeViewItem(DependencyObject item)
{
while ((item = VisualTreeHelper.GetParent(item)) != null)
{
if (item is TreeViewItem)
return (TreeViewItem)item;
}
return null;
}
 
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox tickedCheckBox = sender as CheckBox;
 
TreeViewItem tviSubLayer = GetTreeViewItem(tickedCheckBox);
TreeViewItem tviLayer = tviSubLayer.GetParentTreeViewItem();
Layer layer = tviLayer.DataContext as Layer;
 
....... Now you can use all layer properties/methods


But this will not solve the second issue concerning the template which should be different for DynamicMapServiceLayer and for WMSLayer. As Silverlight doesn't support 'ItemTemplateSelector' nor 'DataType', it might not be that easy if your don't create your own hierarchical data stucture.
0 Kudos
MikeTischler
Emerging Contributor
Thanks for the reply.

I ended up using the Silverlight Tableofcontents control from the code gallery, and adapting that.
0 Kudos