|
Instead of implementing the guard as a function, create a CanRemoveItem
property, and use the
NotifyPropertyChanged(()=>CanRemoveItem);
to notify the system about the change in the property value.
The only issue is that your guard requires a parameter (i.e. the item to be removed), so you probably need to re-design that; consider providing a
SelectedItem property, raise the property change notification every time the selected item changes, and modify the
CanRemove implementation to use the SelectedItem instead of the parameter currently passed to the method, or alternatively, move the CanRemoveItem/RemoveItem pair into the view-model associated to the MenuItem.
That said, I suppose that the guard is evaluated:
- when the context menu item is created
- when the context menu item is loaded
hence the two times. If the menu item is never disabled, there is a high chance that the enabling state does not reflect the current guard value.
You can find more info about action guard properties
here.
|