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?

Answers


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.


Need Your Help

Inserting spaces between digits in C

c formatting printf digits

How would I go about taking a number like 123456 and having it print as 1 2 3 4 5 6?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.