Search code examples
c#wpfxamlroutedcommandglobalevent

RoutedCommand for multiple Windows


been looking through Google results for quite some hours now and all i was finding were examples of how to use RoutedCommand to catch MouseClicks and Keyboard Shortcuts to do the same actions.

I've read some sources about RoutedCommand like MSDN and https://joshsmithonwpf.wordpress.com/2008/03/18/understanding-routed-commands/ and if i still understand it right, it should also be able to do what i need:

  1. Click a button in Window2
  2. Call Method in Window2
  3. Call Method in Window1
  4. Call Method in Window3 etc.pp.

The use case would be a Settings window which holds Language settings and upon clicking OK in the settings all active windows should change their language strings. I've put up a Method which reloads the strings with the language identifier.

So in general i want the ButtonClick in Window2 to pass the Command to call Method "ChangeLanguage(lng)" to all active Windows be it the parent MainWindow or another ChildWindow not related to the Settings at all. The Window classes have their own ChangeLanguage() functions to alter their own strings.

I wonder if using a custom EventHandler would be better in this case and somehow make it global so every active Window will listen to the Event which is fired when the language is being changed in the Settings Window. I found an example for a global CommandHandler at another question here but that's not working for an Event i suppose.


Solution

    1. If your view objects are nested withing root control and you click on button placed in root control you can create TunnelingRoutedEvent which will go down visual tree. In Window1, Window2 etc. you handle this event.
    2. You can create static delegate to which methods from Window1, Window2 etc. will join and once delegate is invoked all associated methods are invoked as well.
    3. In case you are using PRISM, CompositeCommand is what you are looking for.