#1,085 – Input Bindings Don’t Require that Element Binds to Command
June 3, 2014 1 Comment
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.
Pingback: Dew Drop – June 3, 2014 (#1789) | Morning Dew