KeyBinding in usercontrol

Topics: Prism v4 - WPF 4
Feb 21, 2011 at 5:39 PM


I've added KeyBindings to a UserControl, this UserControl is added to the Shell using the regionmanager. The UserControl needs to have focus to make the KeyBindings work. Is there any way to make the KeyBindings work without always giving the UserControl focus. Should I use commaind or the eventaggregator ?

I was thinking about creating a "KeyPressEvent" which holds information about the keys pressed and then publish the event in the KeyDown handler of the shell. Any help or advice is appreciated!

Feb 22, 2011 at 7:11 PM


Take into account that in a composite scenario, the command handler might be a view model that does not have any associated elements in the visual tree or is not the focused element.

For this reasons Prism provides guidance to support this kind of scenarios with DelegateCommands (which allows you to call a delegate method when the command is executed) and CompositeCommands (which allows you to combine multiple commands).

On the other hand, Prism also provides you an EventAggregator event mechanism. As MSDN documentation shows: “(…) enables communications between loosely coupled components in the application. This mechanism, based on the event aggregator service, allows publishers and subscribers to communicate through events and still do not have a direct reference to each other.

(…)Consider using the EventAggregator to publish an event across modules and when sending a message between business logic code, such as controllers and presenters

Based on my understanding of your scenario you could use EventAggregator in your approach, since you need to communicate between any focused view and your user control. publishing an event and then subscribe to it in order to fulfill your requirement.

Additionally, you can read more about these topics in Chapter 9: Communicating Between Loosely Components

Please let me know if this information helps you.


Miguel Bronzovic


Feb 23, 2011 at 6:24 AM

Thanks for the repsonse!

So publishing the KeyPressEvent in a view through the EventAggregator and subscribing to this it from for example the viewmodel would be a good approach to solve this problem ?