Menu and Context Menu with RoutedCommand

The below article describes how to add Menu, Context Menu for WPF application. It also gives solution for the problem of RoutedCommand during execution.

The following example shows the problem with simple window that has a Menu and a ContextMenu on it.


In the codebehind, CommandBinding to handle the Menu item command as below:


Reason:

The CommandManager searches for CommandBindings within the current focus scope. If the current focus scope has no command binding, it transfers the focus scope to the parent focus scope. When you startup your application the focus scope is not set.


The simplest solution is to initially set the logical focus of the parent window that is not null. When the CommandManager searches for the parent focus scope it finds the window and handles the CommandBinding correctly.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.