1

I'm a newby of WPF, and 'm trying to unserstund RoutedCommands. RoutedCommands starts from command source, than event is propagated vua bubbling and tunneling.

But can i stop the process? for example i would like set CanExecute = false and than stop event propagation.

Thanks for any help!

Clemens
  • 123,504
  • 12
  • 155
  • 268
stefano m
  • 4,094
  • 5
  • 28
  • 27

1 Answers1

0

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/>
HCL
  • 36,053
  • 27
  • 163
  • 213