keyboard focus between two modules

Dec 10, 2008 at 10:12 AM
Edited Dec 10, 2008 at 10:20 AM

I think this question is simple, but i can not figure it out by myself:-/

- I have two modules "A" and "B" - both are loaded.

- Module A references module B - (because Module "A" loads "B" ) 

A loads B doing this: 

// IRegion r = _regionManager.Regions["MainArea"];
// if (r != null)
// {
//    r.RemoveAllOtherControlsFromRegion();
//    ModuleB.MainView mlgv = new ModuleB.MainView(_unityContainer, _regionManager);
//    r.Add(mlgv);


My MainView (module B) takes keyboard focus during startup. 

What i am now trying to do is to give back the keyboard focus ( e.g. pressing a button) to a certain focusable element in my viewA (module A).

Is commanding the right way ?  Can someone provide me a code snipset ?

best regards


Dec 12, 2008 at 10:58 AM



You can create a globally available command to accomplish that task by creating an instance of the CompositeCommand class and expose it through a static class. The CompositeCommand class supports programmatic registration of child commands like a DelegateCommand. When the composite command is executed, all the child commands get executed.


To create a globally available command follow these steps:

1.       Create a static class to hold your command instance. You typically create this class inside a common infrastructure project.

2.       Add a public CompositeCommand instance to your static class:


public static class GlobalCommands


public static CompositeCommand MyCompositeCommand = new CompositeCommand();



3.       Add an invoker to the global command and associate it to a control in your MainView (module B) in the following way:


<Button Name="MyCompositeCommandButton" Command="{x:Static local:GlobalCommands.MyCompositeCommand}">Execute Command</Button>


4.       In the ViewA (module B) where you want to get focus, implement a method that contains the logic to be executed when the command is invoked (get focus on an element). This method must return void and take a single parameter:


public void OnMyCommandExecute(MyParameter parameter)


// TODO: Implement business logic for the command.



//NOTE: MyFocusableElement is the control that you want to focus on.



5.       Add a variable definition to your class of type DelegateCommand<T>, where T is the type of the parameter the methods you implemented in the previous steps. When creating an instance of the DelegateCommand, pass as a parameter in its constructor the handler of the method defined in the previous step:


DelegateCommand<MyParameter> myCommand = new DelegateCommand<MyParameter>(OnMyCommandExecute);


6.       Finally, associate the previous command (myCommand) to the globally available command. To do this, access the static instance of the command and register the child command with the RegisterCommand method:




Perhaps you could find useful the following articles in the documentation.

·         Commands

·         How to: Create Globally Available Commands

·         How to: Create Locally Available Commands


Please let me know if this helps.


Mariano Converti

Dec 15, 2008 at 1:01 PM
Hi Mariano ,
thanks for your reply - some things become clear.
Tomorrow i will give it a try :-)

best regards