I am trying to restrict an add-in from working on the basemap layer and the top layer in the TOC. The add-in shows in the right click menu of a layer. The code I have disables the add-in correctly if you left click the layer to select and then right click to get the context menu. However, if you right click another layer to select and open the right click menu at the same time it is not working correctly. It still at the previously selected layer to see if the add-in can execute. If I right click the layer again it works fine. It seems like the right click menu is appearing before the canexecute event finishes. Is it something I am doing wrong in my code?
namespace LayerOrder.AddIns
{
[Export(typeof(ICommand))]
[DisplayName("Move Layer Up")]
[ESRI.ArcGIS.Client.Extensibility.Description("Move selected layer up.")]
public class MoveLayerUp : ICommand
{
#region ICommand members
public bool CanExecute(object parameter)
{
Layer myselectedlayer = MapApplication.Current.SelectedLayer;
int mylayerid = MapApplication.Current.Map.Layers.IndexOf(myselectedlayer);
int layercount = MapApplication.Current.Map.Layers.Count;
if (mylayerid == 0)
return false;
else if (mylayerid == (layercount - 1))
return false;
else
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
Layer selectedlayer = MapApplication.Current.SelectedLayer;
int layerid = MapApplication.Current.Map.Layers.IndexOf(selectedlayer);
MapApplication.Current.Map.MoveLayer(layerid, layerid + 1);
}
#endregion
}
}