Select to view content in your preferred language

Propogate Legend Selections up Legend Tree

737
3
Jump to solution
04-17-2013 12:42 PM
DaveOrlando
Frequent Contributor
I am looking for some code to propogate Legend Selection up the Legend tree.

I've been experimenting with the LayerItemViewModel and LegendItemViewModel and LayerItems / Layers etc etc but can't quite get it.

any help would be great.

just to clarify, when a user opens many tree nodes and finally finds the layer they want and click it, now they have to go back up the tree and turn every parent on manually to actually see the layer. Of course many don't realize this and say 'nothing shows up, where is the data...' Many requests to improve this functionality.


Thanks,
0 Kudos
1 Solution

Accepted Solutions
DaveOrlando
Frequent Contributor
Okay, we think we have the problem tackled, here is some code to turn on parent items when a subitem is clicked on.

XAML for Legend (Sublayers)
<!--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>


c# for chkLaterItemVis_Click and turnOnParents
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);                         }                     }                 }             }          }


I hope others find this helpful, it has been working great for us

View solution in original post

0 Kudos
3 Replies
DominiqueBroux
Esri Frequent Contributor
This legend 'Google' style sample looks like what you need.
0 Kudos
DaveOrlando
Frequent Contributor
Thanks for the reply,

This is some cool functionality, but a little overkill for the goal I'm trying to accomplish. No need to turn all on or off from the parent.

all i really need is some code to detect what parent the sublayer is part of and matched it's visibility (checked state). from the REST point each layer clearly states what the parent ID is, but a little harded to track down from the legend control.

I tried to use some of your C# in the example for detecting parent layers but i don't think it was intended for that

anyways, any more hints would still be appreciated.

Thanks
0 Kudos
DaveOrlando
Frequent Contributor
Okay, we think we have the problem tackled, here is some code to turn on parent items when a subitem is clicked on.

XAML for Legend (Sublayers)
<!--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>


c# for chkLaterItemVis_Click and turnOnParents
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);                         }                     }                 }             }          }


I hope others find this helpful, it has been working great for us
0 Kudos