I have added the ability to dynamically add an ArcGISDynamicLayer Map Service to the map and to the TOC/Legend. But, it places it at the bottom of the legend, and I would like to place it at the top.
private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e) { Legend legend = sender as Legend; if (e.LayerItem.Layer.ID == "MyTopLayerID") { legend.LayerItems.Remove(e.LayerItem); // Remove legend entry from its current location legend.LayerItems.Insert(0, e.LayerItem); //Add it at the top } }
Hey Dominique,
I can successfully remove a legend entry with your example above. How would I go about re-adding an item after removing it? Once I remove the LayerItem from the Legend l no longer have access to that layer to use the insert method.
Ex:
if(HPGasLegend == false){
if (layerItemVM.Layer.ID == "HP_Gas_Feeder_Layer")
{
Legend.LayerItems.Remove(e.LayerItem);
}
}
else
{
// here is where I would like to reinsert the layer above that I removed based on a checkbox checked/unchecked event
// when I hover over the legend object and look through the collection I no longer see my layer that with the ID of "HP_Gas_Feeder_Layer"
// in the collection
}
Thanks for any help!
The easiest way to get a layer out of the legend is to set the layer.ShowLegend property to false.
So by binding this layer property to your checkbox, you should directly get the expected behavior.
Else, if for any reason you'd rather using the Legend_Refreshed Handler, a couple of possible options:
- call Refresh on the Legend object (the drawback is that all legend layers will be refreshed)
- keep the legend item in a private field when you removed it, so you can readd it when needed.
I was attempting to keep a copy of the legend item and readd it but it seems like it will be overly complicated for multiple layers.
Could you give an example of the binding the layer property to the checkbox?
Thanks!
Nevermind Dominique,
I got it to work with this code. Thank you for your help in pointing me in the correct direction.
<CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="331,261,0,0" VerticalAlignment="Top"
DataContext="{Binding Layers, ElementName=MyMap}"
IsChecked="{Binding [GasLayer].ShowLegend, Mode=TwoWay}" />