private string _weatherleg = "Weather";
if (e.LayerItem.Layer.ID == "Nexrad")
MyLegend.LayerItems.Insert(2, _weatherleg);
where 2 is the index location of placing the text/label I'm inserting. The problem is that I'm getting an error back stating that the LayerItemViewModel won't accept type string.
private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e) { if (e.LayerItem.Layer.ID != "dummy") // add here a test on the layers you want to group { // Create group legend item if not existing var groupLayerItem = myLegend.LayerItems.Where(l => l.Label == "Group").FirstOrDefault(); if (groupLayerItem == null) { groupLayerItem = new LayerItemViewModel(e.LayerItem.Layer) { Label = "Group", LayerItems = new System.Collections.ObjectModel.ObservableCollection<LayerItemViewModel>() }; myLegend.LayerItems.Add(groupLayerItem); } // remove legend item from root and add it to the group item myLegend.LayerItems.Remove(e.LayerItem); groupLayerItem.LayerItems.Add(e.LayerItem); } }
(e.LayerItem.Layer.ID != "dummy")
Why are you using != (does not equal) for the grouping?
I also think the following is an issue because it keeps trying to make it the first or default group:
var groupLayerItem = MyLegend.LayerItems.Where(l => l.Label == "Weather").FirstOrDefault();
although I will say it doesn't like to add the first layer into any group -- not sure why
!= is just a sample working with a negative test excluding some layers from the group. You can obviously use a positive test on the layers to include (and I agree it's better).
I don't understand what you mean. It just a test to know if the group layer has already been created or not.
Anyway, it looks like you succeeded to get the result you were expecting. So 🙂
Strange, the initial sample I provided was adding the first layer in the group. What kind of layer is your first layer?
Also, because I'm using the tree view and not the flat view I'm getting a checkbox for the group label and I don't want one --- I want just the label for the group. Any ideas?