Removing an action

Aug 24, 2013 at 2:53 AM
I have an edit button (thanks to Jim) that puts my form into edit mode. It works quite well.

However, when the user clicks on the button, I need the edit button to go away and the save button to show up. Adding the save button is easy.

The only way I have figured out how to remove the edit button is:
foreach (ViewAction act in Actions)
    if (act.Caption.left(4).ToLower() == "edit")
While this works, I kinda figured there was a better way, so I thought I would ask. I did come up with some alternatives, but none of them would be considered "better".

Aug 24, 2013 at 6:07 PM
Hello Fletcher,
You are right, there is a better way. When you create your view action you can define a boolean property or method to assign to the "canExecute" property of the ViewAction. Depending on the theme the action will either be shown disabled, or in most current themes shown or hidden based on the boolean value.
new ViewAction("Generate",
                execute: (a, o) => Generate(), brushResourceKey: "CODE.Framework-Icon-Copy",
                canExecute: (a, o) => InEditMode
The code above takes care of the action when created, but if you update the property/method in code you will also need to call the "InvalidateAllActions" method that is part of the base ViewModel. This method will cause all actions to re-evaluate their canExecut status and show/hide the action during run time.

Aug 25, 2013 at 6:15 AM

So while my code actually deletes the action, this approach simply makes it disabled. But this can then make it easier to go from view to edit mode and back without having to keep creating and deleting actions. I hadn't quite thought of using canExecute this way.


Sep 4, 2013 at 11:52 AM

the default style shows the button as disabled when canExecute returns false. Note that it is also possible to create themes that hide unavailable buttons completely, if so desired.

Sep 4, 2013 at 4:33 PM

The UI I am trying to create is one where the data is in read only format with an "edit" button. If they click on the edit button, the button is "replaced" with a save button. Other buttons in the toolbar remain unchanged.

One option is to have the viewaction call a method which then figures out what to do (save or edit). Then the template can choose the appropriate icon, tooltip, and text (if turned on) to show. Then CanExecute is used to indicate if they can edit or not (this can change on a record by record basis.) This should work unless you have a better suggestion.

I will look around for it, but if you have a moment and can point me to the appropriate resource dictionary for the toolbar buttons, that would be helpful.


Sep 4, 2013 at 10:34 PM
You mean the resources for the styles that define what the actions look like? That should be part of the Shell.xaml file in each skin. Look for "normal view host", I think. (Without spaces).