This project is read-only.

Removing an action

Aug 24, 2013 at 3: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")
    {
        Actions.Remove(act);
        break;
    }
}
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".

Thanks!
Aug 24, 2013 at 7: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.

Jeff
Aug 25, 2013 at 7:15 AM
Jeff,

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.

Thanks,

Fletcher
Sep 4, 2013 at 12:52 PM
Fletcher,

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.

Markus
Sep 4, 2013 at 5:33 PM
Markus,

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.

Thanks,

Fletcher
Sep 4, 2013 at 11: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).

Markus