[WPF] ContextMenu Commnad binding issue fixed

By | December 17, 2013

Advertisement from Google

If there is no focused element in the window’s main focus scope, the CanExecute routing will stop at the ContextMenu, so it will not reach to the CommandBinding on the Window, one workaround is to bind MenuItem’s CommandTarget to the main window.

Advertisement from Google

<ContextMenu >
      <MenuItem Command="ApplicationCommands.Open"
                  CommandTarget="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>

(c) Marco Zhou 

Leave a Reply

Your email address will not be published. Required fields are marked *