[WPF] ContextMenu Commnad binding issue fixed

By | December 17, 2013

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.

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

(c) Marco Zhou 

Leave a Reply

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