1. Computing

Introduction to Commands in WPF

A WPF Way to Code Common Actions

From , former About.com Guide

See More About
Updated April 11, 2010

Understanding a WPF Command

WPF gives you a new way to write code for actions that can be performed with different kinds of inputs (for example, mouse clicks, keyboard combinations, menu selections). The classic example of something that is implemented using a command in WPF is the Cut or Paste operation. But lots of actions can be coded using commands. Like most things in WPF, you can even make up your own actions and write code so they can be coded using commands too.

Don't let the name confuse you. This is nothing like a DOS "command" or a SQL "command". They just used this name for an idea you have seen before a lot in Object Oriented Programmming. In fact, the only reason they use the word "command" is to get across the idea that this is the way you write the code for actions that you might think of a a single "command" - like "paste". The way you write the code is different, however, and that's the main thing you have to learn. The basic idea behind WPF commands is to separate the code your write at the source of the operation from the code at the target of the operation. In Object Oriented Programming, this idea is a big part of the OOP principals of abstraction, encapsulation, polymorphism, and decoupling.

Commands are actually properties of the controls that support them. The main controls you will use commands with are MenuItem, Button, RadioButton, CheckBox, and Hyperlink.

As an example of a command, consider Paste. This operation can (usually) be done at least four different ways:

  • Using the keyboard combination "Ctrl-v"
  • Using a Paste menu selection
  • Dropping in a "drag and drop" operation.
  • Right-clicking and selecting "Paste"

Furthermore, what actually happens is different depending on where the Paste is done and exactly what is being pasted. For example, if you paste a graphic into an email message, you need entirely different code than if you paste text into the VB.NET code window. But they're both thought of as being "paste".

WPF has a slick way of getting around this problem: Commands.

They're using a new word to describe all of the different ways to trigger a command, too. They're called "gestures". In more traditional coding, you might have to write a different event subroutine for every one of the different ways. With commands, you don't have to do that.

You can write your own code to implement commands, or you can just use code that is already in WPF. And there's a lot of code there already. In fact, there are (currently) five different libraries of commands in WPF.

  • ApplicationCommands - This includes the well-known Cut, Copy, and Paste commands and a lot of others.
  • EditingCommands - Most text-oriented commands are in this library, such as ToggleBold and DecreaseFontSize.
  • NavigationCommands - The name tells you what they are. You find commands like NextPage and Search in this library.
  • ComponentCommands - These commands have a lot in common with some of the other libraries like NavigationCommands and EditingCommands since they include things like MoveDown and ExtendSelectionLeft. The main thing that makes this library different is that they are usually for more general use.
  • MediaCommands - Again, the name is all you need to know. This library has commands like Play and IncreaseVolume.

On the next page, we look at how to actually use a WPF command in code.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Learn VB.NET
  5. Commands in WPF - A WPF Way to Code Common Actions

©2013 About.com. All rights reserved.