Reputation: 7913
In my WPF application, I use Routed Commands quite often, since there are several actions that can be fired from different places in the code.
Now the problem is this: this mechanism works perfectly as long as the main program window has the focus, but if I have a dialog window showing, commands stop working. So if I have a dialog box or window on top of the main window, commands basically stop beeing captured.
Testing a bit further, I found out that the commands themselves are executed, but they aren't beeing routed correctly: in fact if I pass the object that contains the command binding as the "target" parameter in the Command.Execute() method they work... the problem is I don't always have the target at hand (that's why I use routed commands, because they are... well... routed!)
Maybe it's something really stupid I'm not getting... I'm still learning with WPF.
Thanks!
Upvotes: 0
Views: 924
Reputation: 1380
The problem is that the commands mechanism routes the command up the visual tree, that is it travels up the tree from the command target until it finds an element which contains a binding for the command. So when you try to execute the command from your dialog window the topmost element is the dialog window, it has no direct connection with the previous window in the visual tree.
You either have to provide the target yourself or create the binding in the dialog window for it to be handled properly.
If you open that dialog window from the previous window then specify the Owner property and you can use to as the command target.
I hope that helps.
Upvotes: 1