Geeks With Blogs

The Life and Times of a Dev Yes, we're really that weird

In many ways, CAB and WPF have many similar functions.  One area where there's overlap, and a little bit of pain is with WPF commands.  If you're new to commands, here's a great blog post about them.  The Microsoft Help for commands is here.

Suppose the following:

  1. You have a search workspace that contains all of the logic for executing a search and displaying search results and a single search textbox.  When a search result item is selected, you raise a cab event and then stuff happens.
  2. You want to go to the search box any time that a user presses the CTRL+S hotkey combination.  Note that the default key is F3 in windows, but you want to add to that key selection.

A fundamental part of CAB is that you don't know where your smartpart is going to reside inside of the application.  It could be on a form all by itself, it could be just another control on a form with many controls.  If you put the WPF command binding in the search workspace, then the only place that your hot key will work is in the search workspace!  Not exactly what you want, since you'd have to be in the search box to press the hotkey and go to the search box.  What to do, what to do.

Here's the solution.  In the parent workspace (perhaps the application window?), you'll add your command bindings and input bindings.  The code inside of those command and input bindings will simply raise a cab event, which your search workitem will listen for and respond to.  You can attach the command bindings and input bindings to any WPF container and many controls where you want this to happen.

I'm assuming that you use the built in NavigationCommands.Search command.  Note that the F3 gesture is automatically associated with the NavigationCommands.Search command.

Here's how you'd wire up the WPF events:

        <CommandBinding Command="NavigationCommands.Search"
        <KeyBinding Command="NavigationCommands.Search"

Then in the code behind, you'd need to define the following CAB event:

        [EventPublication("Search", PublicationScope.Descendants)]
        public event EventHandler<ExecutedRoutedEventArgs> ExecuteSearchFocus;

Note how I'm constraining this to just the decendents.  This lets you control scope of what receives the search event, so if you had multiple search controls in the same workspace, you could control which received the commands by where the user was when they invoked the gesture. 

You'll also need the following two methods:

       public void OnExecuteSearchFocus(object sender, ExecutedRoutedEventArgs e)
            if (this.ExecuteSearchFocus != null)
                this.ExecuteSearchFocus(sender, e);

        public void CanExecuteSearchFocus(object sender, CanExecuteRoutedEventArgs e)
            e.CanExecute = true;

This would set up the parent workspace so that when CTRL+S was pressed, the CAB Event would be raised notifying any smartparts listening that they should do the search.

As for the listening event, it'll look something like this:


        [EventSubscription("Search", ThreadOption.UserInterface)]
        public void Search(object sender, RoutedEventArgs e)
            //Do search work here


As simple as that, you've got your command wired up to your cab events.

Some important things to note:

  1. You can use the CanExecute method to determine if whatever it is you're attached to will be enabled or not.  I'm always returning true, but you could set it up so that it only returned true if you were inside of a text box or other control.
  2. You can wire up multiple keys to the same command by adding multiple KeyBindings.  Note however, that this just binds two or more keys to the same command.  To bind a key sequence, you'll need to create your own binding object that derives from InputBinding.
Technorati Tags: ,,,
Posted on Thursday, February 14, 2008 1:35 PM | Back to top

Comments on this post: CAB and WPF Commands

# re: CAB and WPF Commands
Requesting Gravatar...
hey movie buffs visit netflix account login for hd movies
Left by daniels danny on Dec 10, 2016 6:28 AM

Your comment:
 (will show your gravatar)

Copyright © Robert May | Powered by: