I suppose you try to stop a RoutedCommand
to bubbling up the visual tree when your CanExecute
-Handler sets CanExecute
to false
? If yes, try the following:
Set e.Handled = true
. Make sure that the CommandTarget
is set correct so that your CanExecute
-Handler really is called.
This is important, if you place your command in a ContextMenu
. If no CommandTarget
is specified, WPF will take the focused element and your CanExecute
-Handler will not always be called and therefore possibly interpreted by another handler.
Something like the following will do the job:
<ContextMenu>
<MenuItem Command="DesiredCommand" CommandTarget="{Binding PlacementTarget,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}" .../>
...
</ContextMenu/>