Select to view content in your preferred language

highlight the active tool

1551
9
08-05-2010 09:36 AM
DaveOrlando
Frequent Contributor
Hello all,
Are there some examples out there which change or highlight the image/button of the active tool.
for example: the user selects my 'select' tool, then have the button highlight, but more importantly, remove the highlight if the tool becomes inactive (like what happens when the user selects an area where there are no features, the actions returns to pan).
Thanks in advance,
Dave
0 Kudos
9 Replies
JenniferNery
Esri Regular Contributor
There is no sample for this yet because the SL/WPF API does not expose the Editor's ActiveEditor property, which you might need to achieve this.  You can however, listen to the Editor's EditorActivated and EditCompleted events to know when the Editor is active and which EditAction has completed. Or you can also listen to the button's Click and IsEnabledChanged events. Either way, in the event handler, you can change your image or button's appearance accordingly.

Jennifer
0 Kudos
DaveOrlando
Frequent Contributor
These are some great suggestions. I have been experimenting all morning and can't get the desired results.
The 'EditorActivated' does not fire when it is UnActivated. As you can feel for yourself in the example (http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureLayerSelection) if the user misses their selection, the editor turns off, but the event is not fired.
In MyMap I am also listening for a MouseClick but that doesn't fire until the second click after missing a selection (one click turns off the editor, then the second click fires the MouseClick on the Map)
Maybe this is not achievable yet. Kindof goes back to the behavior of 'tools' vs 'commands', one stays 'clicked' the other does not.
I appreciate the help, any other suggestions/missing something.
0 Kudos
JenniferNery
Esri Regular Contributor
I know this is not an elegant solution but it is a workaround if you really would like to change the appearance of your button. While the buttons' Command help determine if its state will be enabled, it does not detect when it is active. The Editor will know which command is active but it this is not made public. The EditCompleted event will help you decide which active button to turn off once it's complete.

Using that specific example, you can do the following.

Listen to EditCompleted. This will be useful for removing highlight on the active button.
        <esri:Editor x:Key="MyEditor"
                         Map="{Binding ElementName=MyMap}"
                         LayerIDs="CensusDemographics"
                         SelectionMode="Rectangle"
                         ContinuousMode="True"
                         EditCompleted="Editor_EditCompleted"/>


and for each button, listen to Button_Click. This will be useful for adding highlight to the active button and removing highlight to the other buttons if Editor is ContinuousMode. Do this for each of the buttons.

  <Button x:Name="SelectButton"
                            Margin="2"
                            Content="New"
                            Command="{Binding Select}"
                            CommandParameter="New"
                            Click="SelectButton_Click"
                            ></Button>


In the code-behind, you can do something like this.

        private void Editor_EditCompleted(object sender, Editor.EditEventArgs e)
        {
            Editor editor = sender as Editor;
            if (editor.ContinuousMode) return; // for this case, remove highlight when other button is clicked.
            foreach (var edits in e.Edits)
            {
                if (e.Action == Editor.EditAction.Select)
                {
                    SelectButton.Background = new SolidColorBrush(Colors.LightGray);
                }
            }
        }

        private void SelectButton_Click(object sender, RoutedEventArgs e)
        {           
            //if Editor is Continuous, remove highlight on other buttons before this code
            Button b = sender as Button;
            b.Background = new SolidColorBrush(Colors.Yellow);
        }


I hope this helps.

Jennifer
0 Kudos
DaveOrlando
Frequent Contributor
Thanks, I always learn lots from seeing code samples.

This code is great for dealing with the button 'highlights', but I think my underlying goal goes a bit deeper. Somehow can we detect when the Editor is active and/or turn if off with a toggle? (I guess by your first post -no) I can't believe that the editor turns off just because the user clicks somewhere other than the selectable layer, and on the flip side, if they want to turn off the selection process they have to click somewhere other than this layer.

I hope I'm just missing something, but if not just tell me and I'll let it go......for now.
0 Kudos
JenniferNery
Esri Regular Contributor
If you only want to know when the Editor is activated, you can listen to the EditorActivated event. But if you need to know which command is currently active, you cannot get this information from this event. You can investigate what properties and methods are available in the Editor.

Currently, there is no property to tell you which command will begin to execute when this event is fired. This is why I suggested, using Click events. Because what really goes on here is first user will click on the button, EditorActivated event is fired, and the command for that button gets executed; so on click event you know which command user has just activated. This is just a proposed workaround.

I am a little bit confused with this one, can you clarify what you mean?

I can't believe that the editor turns off just because the user clicks somewhere other than the selectable layer, and on the flip side, if they want to turn off the selection process they have to click somewhere other than this layer.


Do you have something like this, where each of the button's click event is wired up to the Button_Click? You can replace the code that goes in these event handlers. This is just to show the simple case where you want to highlight the active button.
        public MainPage()
        {
            InitializeComponent();
            editor = this.LayoutRoot.Resources["MyEditor"] as Editor;
            if (editor != null)                
                editor.EditCompleted += new EventHandler<Editor.EditEventArgs>(editor_EditCompleted);                
        }
        Editor editor;
        void editor_EditCompleted(object sender, Editor.EditEventArgs e)
        {
            if (editor.ContinuousMode) return; 
            foreach (var edits in e.Edits)
            {
                if (e.Action == Editor.EditAction.Select)               
                    ResetButtonHighlights();
            }
        }

        private void ResetButtonHighlights()
        {
            SelectButton.Background = new SolidColorBrush(Colors.LightGray);
            AddSelectButton.Background = new SolidColorBrush(Colors.LightGray);
            RemoveSelectButton.Background = new SolidColorBrush(Colors.LightGray);
            ClearSelectionButton.Background = new SolidColorBrush(Colors.LightGray);
            EnableKeyboardButton.Background = new SolidColorBrush(Colors.LightGray);
        }
        
        private void Button_Click(object sender, RoutedEventArgs e)
        {            
            if (!editor.ContinuousMode)
                ResetButtonHighlights();
            Button b = sender as Button;
            b.Background = new SolidColorBrush(Colors.Yellow);
        }


Jennifer
0 Kudos
JenniferNery
Esri Regular Contributor
Oh I think I get what you are talking about now. You are looking for an event that will fire when the command is no longer active.

EditorActivated only fires when you activate a command and EditCompleted event only fires when you have successfully executed the command.

For the case when you try to make a selection on an area without graphics, no selection is made, the command did not execute successfully so EditCompleted is not raised.

I'm afraid that using only these two events cannot get you what you wanted.

Jennifer
0 Kudos
DaveOrlando
Frequent Contributor
Yes, exactly. The editor will turn off but there is nothing to detect to fire a 'ResetButtonHighlights'.

That is why I have also been listening to the Map's Click event, but that does not fire either until the next click after the editor turns off.

Thanks again for the code, it is great. I will try a few more things today.
0 Kudos
DanDulan
Emerging Contributor
Try this:

      private void FeatureLayer_MouseEnter(object sender, GraphicMouseEventArgs e)
        {
            SelectButton.IsEnabled = true;
            AddSelectButton.IsEnabled = true;
            RemoveSelectButton.IsEnabled = true;
            ClearSelectionButtonIsEnabled = true;
            EnableKeyboardButton.IsEnabled = true;

        }

        private void FeatureLayer_MouseLeave(object sender, GraphicMouseEventArgs e)
        {
            SelectButton.IsEnabled = false;
            AddSelectButton.IsEnabled = false;
            RemoveSelectButton.IsEnabled = false;
            ClearSelectionButtonIsEnabled = false;
            EnableKeyboardButton.IsEnabled = false;
        }


Thanks,
Dan
0 Kudos
DaveOrlando
Frequent Contributor
Oh I think I get what you are talking about now. You are looking for an event that will fire when the command is no longer active.
EditorActivated only fires when you activate a command and EditCompleted event only fires when you have successfully executed the command.
For the case when you try to make a selection on an area without graphics, no selection is made, the command did not execute successfully so EditCompleted is not raised.
I'm afraid that using only these two events cannot get you what you wanted.
Jennifer


The beta 2.1 API has resolved this issue. ie The editor will not turn off unexpectedly if the user makes a 'null' selection. Great work.
0 Kudos