Handling standard commands in a user control
A typical user control exposes properties and events, but it can also handle commands. These can be standard
commands (RoutedUICommand
s defined by WPF) or custom commands exposed as properties and invoked by the control. In this recipe, we'll see how to add command handling to a user control.
Getting ready
We'll use the projects we created in the previous recipe, Creating a user control, so make sure the solution is open.
How to do it...
We'll add handling for the standard MediaCommands.ChannelUp
and MediaCommands.ChannelDown
commands. ChannelUp
will increase each of the RGB values and ChannelDown
will decrease them.
Open
ColorPicker.xaml.cs
. Add a static constructor to the class that registers for command handling:static ColorPicker() { CommandManager.RegisterClassCommandBinding( typeof(ColorPicker), new CommandBinding( MediaCommands.ChannelUp, ChannelUpExecute, ChannelUpCanExecute)); CommandManager.RegisterClassCommandBinding...