Solved! Go to Solution.
<!--Layers within MapLayers--> <esriToolkit:Legend.LayerTemplate> <DataTemplate> <CheckBox x:Name="chkLayerItemVis" Content="{Binding Label}" IsChecked="{Binding IsEnabled, Mode=TwoWay}" IsEnabled="{Binding IsInScaleRange}" Click="chkLayerItemVis_Click" Tag="{Binding SubLayerID}"> </CheckBox> </DataTemplate> </esriToolkit:Legend.LayerTemplate>
private void chkLayerItemVis_Click(object sender, RoutedEventArgs e) { // Goal is to set visibility of parent Layers if child layer is checked. CheckBox chk = sender as CheckBox; // Only concerned if they checked the layer 'on' if (chk.IsChecked == true) { // At this time - we know the 'name' and 'SubLayerID' (bound SubLayerID to chk's 'Tag' in XAML) of the checked layer (child) // this will result in all parent layers being turned on, if they have a child with the same name and ID as // what was clicked. // // Because we are using name AND ID (and IDs don't double up), this will only happen if multiple DynamicLayers layers have the same 'name / ID' child combos // // you can do the same with only 'name', but parents will turn on if they have a child with the same name as what was clicked // string NameOfLayerChecked = chk.Content.ToString(); int IDOfLayerChecked = (int)chk.Tag; foreach (Layer l in MyMap.Layers) { // Only concerned about Dynamic Layers - only ones that would be checked. if (l is ArcGISDynamicMapServiceLayer) { ArcGISDynamicMapServiceLayer dl = l as ArcGISDynamicMapServiceLayer; foreach (LayerInfo vLayerInfo in dl.Layers) //if (NameOfLayerChecked == vLayerInfo.Name) // optional - use name only, see note above // if (NameOfLayerChecked == vLayerInfo.Name && IDOfLayerChecked == vLayerInfo.ID) { // We have found a Layer in the Dynamic Map Service that matches // the name and ID of the layer box checked // Set the root to on dl.Visible = true; // set layerID and pass on int turnedOnLayerID = vLayerInfo.ID; turnOnParents(dl, turnedOnLayerID); } } } } } private void turnOnParents(ArcGISDynamicMapServiceLayer pLayer, int pChildID) { ArcGISDynamicMapServiceLayer dl = pLayer as ArcGISDynamicMapServiceLayer; int tParentLayerID; int tID; foreach (LayerInfo vLayerInfo in dl.Layers) { // Check to see if there are any sublayers if (vLayerInfo.SubLayerIds != null) { // Loop through Sub Layers - and see if any of them have our child for (int q = 0; q < vLayerInfo.SubLayerIds.Length; q++) { tID = vLayerInfo.SubLayerIds; if (tID == pChildID) { // We found a Parent that has our child // set visibility tParentLayerID = vLayerInfo.ID; dl.SetLayerVisibility(tParentLayerID, true); // Check to see if Parent has Grandparrents... turnOnParents(pLayer, tParentLayerID); } } } } }
<!--Layers within MapLayers--> <esriToolkit:Legend.LayerTemplate> <DataTemplate> <CheckBox x:Name="chkLayerItemVis" Content="{Binding Label}" IsChecked="{Binding IsEnabled, Mode=TwoWay}" IsEnabled="{Binding IsInScaleRange}" Click="chkLayerItemVis_Click" Tag="{Binding SubLayerID}"> </CheckBox> </DataTemplate> </esriToolkit:Legend.LayerTemplate>
private void chkLayerItemVis_Click(object sender, RoutedEventArgs e) { // Goal is to set visibility of parent Layers if child layer is checked. CheckBox chk = sender as CheckBox; // Only concerned if they checked the layer 'on' if (chk.IsChecked == true) { // At this time - we know the 'name' and 'SubLayerID' (bound SubLayerID to chk's 'Tag' in XAML) of the checked layer (child) // this will result in all parent layers being turned on, if they have a child with the same name and ID as // what was clicked. // // Because we are using name AND ID (and IDs don't double up), this will only happen if multiple DynamicLayers layers have the same 'name / ID' child combos // // you can do the same with only 'name', but parents will turn on if they have a child with the same name as what was clicked // string NameOfLayerChecked = chk.Content.ToString(); int IDOfLayerChecked = (int)chk.Tag; foreach (Layer l in MyMap.Layers) { // Only concerned about Dynamic Layers - only ones that would be checked. if (l is ArcGISDynamicMapServiceLayer) { ArcGISDynamicMapServiceLayer dl = l as ArcGISDynamicMapServiceLayer; foreach (LayerInfo vLayerInfo in dl.Layers) //if (NameOfLayerChecked == vLayerInfo.Name) // optional - use name only, see note above // if (NameOfLayerChecked == vLayerInfo.Name && IDOfLayerChecked == vLayerInfo.ID) { // We have found a Layer in the Dynamic Map Service that matches // the name and ID of the layer box checked // Set the root to on dl.Visible = true; // set layerID and pass on int turnedOnLayerID = vLayerInfo.ID; turnOnParents(dl, turnedOnLayerID); } } } } } private void turnOnParents(ArcGISDynamicMapServiceLayer pLayer, int pChildID) { ArcGISDynamicMapServiceLayer dl = pLayer as ArcGISDynamicMapServiceLayer; int tParentLayerID; int tID; foreach (LayerInfo vLayerInfo in dl.Layers) { // Check to see if there are any sublayers if (vLayerInfo.SubLayerIds != null) { // Loop through Sub Layers - and see if any of them have our child for (int q = 0; q < vLayerInfo.SubLayerIds.Length; q++) { tID = vLayerInfo.SubLayerIds; if (tID == pChildID) { // We found a Parent that has our child // set visibility tParentLayerID = vLayerInfo.ID; dl.SetLayerVisibility(tParentLayerID, true); // Check to see if Parent has Grandparrents... turnOnParents(pLayer, tParentLayerID); } } } } }