Select to view content in your preferred language

CanExecute selected Layer

2411
1
06-20-2012 08:16 AM
BrianLeroux
Frequent Contributor
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
    }
}
0 Kudos
1 Reply
JeffMcConnell
Occasional Contributor
I've run into the same issue and I don't have a work around, but there is a built in tool that will re-order layers and it seems to work properly when right clicking.  Perhaps someone with access to the source could chime in and let us know how to update the seleced layer when right clicking.

p.s. I am using the layer class to determine the CanExecute status.

        public bool CanExecute(object parameter)
        {
            if (MapApplication.Current.SelectedLayer is FeatureLayer)
            {
                QueryFeatureLayerDialog.selectedLayer = MapApplication.Current.SelectedLayer;
                return true;
            }
            else if (MapApplication.Current.SelectedLayer is ArcGISDynamicMapServiceLayer)
            {
                return true;
            }
            else
            {
                MapApplication.Current.HideWindow(queryFeatureUI);
                QueryFeatureLayerDialog.selectedLayer = null;
                return false;
            }
        }
0 Kudos