Select to view content in your preferred language

How do I get the selected LegendItem?

4345
11
12-08-2010 01:47 PM
KirkKuykendall
Deactivated User
When I use the templated legend sample, I see that there is either zero or one selected item in the tree at any given time.

Is there a method on the Legend that returns the currently selected LayerItemViewModel?
0 Kudos
11 Replies
DominiqueBroux
Esri Frequent Contributor
Try with something like:
 
Public NotInheritable Class LegendExtension
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension> _
Public Shared Function SelectedItems(legend As Legend) As IEnumerable(Of LegendItemViewModel)
If legend Is Nothing Then
Return Nothing
End If
Return legend.LayerItems.Descendants().Where(Function(item) item.IsSelected)
End Function
 
<System.Runtime.CompilerServices.Extension> _
Private Shared Function Descendants(layerItems As IEnumerable(Of LayerItemViewModel)) As IEnumerable(Of LegendItemViewModel)
Dim result As New List(Of LegendItemViewModel)()
If layerItems Is Nothing Then
Return result
End If
 
For Each layerItem As var In layerItems
result.Add(layerItem)
 
' return legend items
If layerItem.LegendItems IsNot Nothing Then
result.AddRange(layerItem.LegendItems)
End If
 
' return recursively layer items
If layerItem.LayerItems IsNot Nothing Then
result.AddRange(layerItem.LayerItems.Descendants())
End If
Next
Return result
End Function
End Class
 
0 Kudos
abdulrenish
New Contributor
The extension is a good suggestion. For me, i have two map controls, and I use a same Legend (TOC) for selecting layers for the two Maps. When i click on First Map, the legend will popup there and i can select one layer for map 1. And when i right click on mp 2, same legend will popup there, but here I need to disable the Map1 selected layer. Please find me a solution.
0 Kudos