#1,085 – Input Bindings Don’t Require that Element Binds to Command

When binding a routed command in WPF to a user interface element, you typically do the following:

  • Create a CommandBinding instance that associates a command to executable code
  • Add the CommandBinding to the element’s CommandBindings collection (typically belonging to the top-level window)
  • Set the Command property of an individual user interface element to refer to the Command

When you define InputBindings, you must perform the first two steps listed above.  You do not have to set the Command property of any user interface element in order for the input binding to work.

Consider the following example:

  • We bind a command to some code by creating a CommandBinding
  • We add the CommandBinding to a window’s CommandBindings collection
  • We define a KeyBinding for the same command and add to window’s InputBindings

We can now execute the command using the key, even though we didn’t set the Command property for any UI element.

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #1,085 – Input Bindings Don’t Require that Element Binds to Command

  1. Pingback: Dew Drop – June 3, 2014 (#1789) | Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: