Its tasty command updating wpf canexecute not ASS

Sunday, April 01, 2018 by Axim

The example Use custom command bindings in WPF and C# explains how you can make a program use custom commands. (This is a confusing topic so read that post before you continue with this one.)

That technique doesn’t quite work if you want to use global command bindings to manage controls on multiple windows. For example, suppose your application should be able to perform some global task. The previous example shows how to use commands to manage that task on a single window, but it doesn’t work if you want to allow controls on multiple windows to participate. Fortunately it’s not too hard to modify that example so it works.

Before I show how to do that, let me explain this example a bit. When it loads, the main window creates a secondary window of type . That class defines the following public field.

The main window uses this code to create and display the secondary window.

This code creates the , sets its field to refer to the main window, and displays the window. (I’ll describe the omitted statements shortly.)

The main window defines the following public field.

Both windows’ Toggle Allow buttons toggle this value. For example, the secondary window’s Toggle Allow button uses this code.

Now let me talk about the command. The following code shows the main window’s complete event handler.

This code creates the command more or less the same way the previous example did. It makes a gesture collection, defines the Ctrl+B gesture, and uses the collection to create the .

It then creates a object and associates the command with its and event handlers. The code then adds the to the window’s collection and sets the button’s property to the command.

Next comes the new stuff. After it creates the secondary window, the code sets that window’s button’s property to the same command. The line highlighted in blue then adds the binding to the secondary window’s collection. I you omit that step, the secondary window doesn’t receive notification when the command’s event handler fires so it can’t update its Background button.

The rest of the example is similar to the previous one. The following code shows the command’s and event handlers.

These event handlers are fairly straightforward. This example only attaches two buttons to the ChangeBackground command, but you can add any number of other buttons, menu items, and other controls to it.

For additional details, see the previous example and download the example.


     

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.

View all posts by RodStephens →

This entry was posted in events, user interface and tagged ApplicationCommands, C#, C# programming, command bindings, CommandBindings, commands, custom commands, disable, enable, events, example, example program, user interface, Windows Forms programming. Bookmark the permalink.

http://csharphelper.com/blog/2015/09/use-global-command-bindings-in-wpf-and-c/