In WPF how can I forward a Command to a child control?
I've got a MVVM-like situation (not using any frameworks) where I have a main window with a toolbar and a documents area. The documents have command bindings (e.g. Undo and Redo). When the keyboard focus is outside of the document, it doesn't know how to execute any of the commands, so the buttons become disabled.
What I would like to do is add a mechanism whereby the main window will forward commands to the active document (if it has one). Using CommandManager.AddPreviewCanExecuteHandler I can get the events in the main window, but I can't figure out how to send them on to the child document- I tried e.CanExecute = e.Command.CanExecute(CurrentDocument), but that just ends up re-calling the PreviewCanExecuteHandler again, and the child's CanExecute handler isn't called.
How can I send an arbitrary ICommand to a child control?
The answer is here- call CurrentDocument.RaiseEvent(e), making sure to do it via a handler registered with CommandManager.AddCanExecuteHandler (not AddPreviewCanExecuteHandler). And make sure to check for infinite recursion.