0

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.

Community
  • 1
  • 1
Fruckley
  • 3
  • 3

1 Answers1

0
  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.
Maximus
  • 3,458
  • 3
  • 16
  • 27
  • 1. Well this works in one direction only.. so when i have Window3 open from Window1 it won't notice these changes. At least that's what i figured by reading all the stuff. 2.What do you mean by Windows will "join there"? – Fruckley Jul 16 '15 at 14:25
  • For starters, there should be only single Window in appliction. Others views are supposed to be UserControls. Delegates are like lists, you can add new and when you invoke delegate all item within are called, in this case all method registered. – Maximus Jul 16 '15 at 14:26
  • Yeah i read about that people suggest using only one Window and several UserControls. But so far changing the whole structure on that is not my target. Would rather prefer an EventHandler that is listened to by every window. – Fruckley Jul 16 '15 at 14:31
  • Applications can use several Windows, there's nothing wrong with that. – almulo Jul 16 '15 at 14:56
  • RoutedEvents are intended for being used inside a Window, though, they don't propagate between Windows. You'll have to use some kind of Publish/Subscribe or EventAggregator patterns. – almulo Jul 16 '15 at 15:12
  • You just pointed out one of the reasons for keeping single window. – Maximus Jul 16 '15 at 15:31