RoutedCommand Focus Scope

When you use RoutedCommand, the most important thing is to make sure that the command will route in the way you want, meaning that the command can find the commandbindng from which the corresponding action will be executed. However, although it also has routing capability, it routes somehow differently from RoutedEvent. Ernie pointed the possible problem when using RoutedCommand, and the solutions.

Apparently, while CommandTarget works fine in one target situation, FocusManager.IsFocusScope seems the only choice for multi-targets circumstance. Unfortunately, Ernie did not explain in detail how IsFocusScope works. Brian did the job. He said:

“Usually, a command invoker looks for a command binding between its own location in the visual tree and the root of the visual tree. If it finds one, the bound command handler will determine whether the command is enabled and will be called when the command is invoked. If the command is hooked up to a control inside a toolbar or menu (or, more generally, a container that sets FocusManager.IsFocusScope = true), then some additional logic runs that also looks along the visual tree path from the root to the focus element for a command binding.”

NOTE: It only works for the commands which check the focus status to decide CanExecute, such as Cut and Copy. For commands of other kinds, it is useless.

I believe the above quoted words can explain how it works in Ernie post.

~ by Martin on November 9, 2008.

Leave a Reply