How do I get the selected LegendItem?

2798
11
12-08-2010 01:47 PM
KirkKuykendall
Occasional Contributor III
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
JenniferNery
Esri Regular Contributor
We currently do not have SelectionChanged event, you can subscribe to MouseLeftButtonDown event on the container inside DataTemplate and get the LayerItemViewModel this way:
var layerItemViewModel= ((sender as StackPanel).DataContext as ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel);
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The LegendItemViewModel class has a property called 'IsSelected', so, despite that there is no 'SelectionChanged' event (as Jennifer mentionned) if you want to get the selected items, you can loop on all legend items and test this property.

The touchy part is that you have to go though the legend items tree hierarchy.

Example of legend extension class returning the selected items:
public static class LegendExtension
{
    public static IEnumerable<LegendItemViewModel> SelectedItems(this Client.Toolkit.Legend legend)
    {
        if (legend == null)
            return null;
        return legend.LayerItems.Descendants().Where(item => item.IsSelected);
    }
 
    private static IEnumerable<LegendItemViewModel> Descendants(this IEnumerable<LayerItemViewModel> layerItems)
    {
        if (layerItems == null)
            yield break;
 
        foreach (var layerItem in layerItems)
        {
            yield return layerItem;
 
            // return legend items
            if (layerItem.LegendItems != null)
            {
                foreach (var item in layerItem.LegendItems)
                    yield return item;
            }
 
            // return recursively layer items
            if (layerItem.LayerItems != null)
            {
                foreach (var item in layerItem.LayerItems.Descendants())
                    yield return item;
            }
        }
    }
}
Then you can get the Selected items by 'MyLegend.SelectedItems()'.


I see that there is either zero or one selected item in the tree at any given time.

That's true with the default legend template, but we can imagine that one changes it to allow multiple selection. It's why my 'SelectedItems' returns a collection, but you can simplify to only one item if you assume that only one item is selectable at once.


0 Kudos
AndrewWhite
New Contributor
Here's how I worked around it.

I added a button to the LayerTemplate and on the click event handler, I store the LayerItemViewModel that was clicked.
0 Kudos
KirkKuykendall
Occasional Contributor III
Thanks for the help!
0 Kudos
JDog
by
New Contributor
Hi Dominique,
I would like to implement this feature.  How or where would I go about plugging this into the Silverlight Toolkit Legend Control?  Sorry, I'm new to the Silverlight world as well as the Toolkit.

Any help would be appreciated!

Regards,
JDog
0 Kudos
DominiqueBroux
Esri Frequent Contributor
To get the selected Legend Item(s), you have first:
     1) to put in your project the LegendExtension class code that I gave in this thread
     2) call MyLegend.SelectedItems() when you want to get the selected items

You don't have to change the toolkit project, it's only in your project.
0 Kudos
JDog
by
New Contributor
Thanks Dominique,
Anybody know a quick fix to port the code to VB.net?
0 Kudos
dotMorten_esri
Esri Notable Contributor
0 Kudos
JDog
by
New Contributor
Thanks SharpGIS,
Unfortunately that doesn't convert the "yield" C# which happens to be the difficult part translating to VB.
0 Kudos