This article is currently in the process of being translated into Vietnamese (~29% done).


Introduction to WPF Commands

Ở các chương trước của bài hướng dẫn này, chúng ta đã nói về cách bắt sự kiện, ví dụ như người dùng bấm bào một nút hoặc danh sách các mục (menu item). Trong giao diện người dùng, nó là kiểu chức năng cho phép truy cập ở một vài nơi, được gọi bởi các hành động khác nhau của người dùng.

Lấy ví dụ, nếu bạn có giao diện người dùng điển hình với danh sách chức năng và thanh công cụ, các hành động như kiểu: Tạo mới hoặc mở có sẵn trong danh sách trong thanh công cụ, trong danh mục (Ví dụ như nhấn trái chuột vào khu vực ứng dụng chính) và phím tắt từ bàn phím như Ctrl+N và Ctrl+O.

Tương ứng với từng hành động sẽ tương ứng với chính xác từng đoạn mã, trong các ứng dụng WinForm, bạn sẽ phải định nghĩa các sự kiện cho chúng và sau đó gọi các hàm (function). Với ví trụ trước, có ít nhất 3 sự kiện và vài đoạn mã để xử lý phím tắt. Nó không phải ý tưởng tốt.


With WPF, Microsoft is trying to remedy that with a concept called commands. It allows you to define actions in one place and then refer to them from all your user interface controls like menu items, toolbar buttons and so on. WPF will also listen for keyboard shortcuts and pass them along to the proper command, if any, making it the ideal way to offer keyboard shortcuts in an application.

Commands also solve another hassle when dealing with multiple entrances to the same function. In a WinForms application, you would be responsible for writing code that could disable user interface elements when the action was not available. For instance, if your application was able to use a clipboard command like Cut, but only when text was selected, you would have to manually enable and disable the main menu item, the toolbar button and the context menu item each time text selection changed.

With WPF commands, this is centralized. With one method you decide whether or not a given command can be executed, and then WPF toggles all the subscribing interface elements on or off automatically. This makes it so much easier to create a responsive and dynamic application!

Command bindings

Commands don't actually do anything by them self. At the root, they consist of the ICommand interface, which only defines an event and two methods: Execute() and CanExecute(). The first one is for performing the actual action, while the second one is for determining whether the action is currently available. To perform the actual action of the command, you need a link between the command and your code and this is where the CommandBinding comes into play.

A CommandBinding is usually defined on a Window or a UserControl, and holds a references to the Command that it handles, as well as the actual event handlers for dealing with the Execute() and CanExecute() events of the Command.

Pre-defined commands

You can of course implement your own commands, which we'll look into in one of the next chapters, but to make it easier for you, the WPF team has defined over 100 commonly used commands that you can use. They have been divided into 5 categories, called ApplicationCommands, NavigationCommands, MediaCommands, EditingCommands and ComponentCommands. Especially ApplicationCommands contains commands for a lot of very frequently used actions like New, Open, Save and Cut, Copy and Paste.


Commands help you to respond to a common action from several different sources, using a single event handler. It also makes it a lot easier to enable and disable user interface elements based on the current availability and state. This was all theory, but in the next chapters we'll discuss how commands are used and how you define your own custom commands.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!