WPF Command Overview

Introduction

The WPF Command provides a mechanism for binding action with a view in Model View ViewModel(MVVM) pattern. In this blog, we will discuss what is WPF Command and how to use it in WPF using WPF ICommand.

Getting Started

Command in Windows Presentation Framework(WPF) with Model View ViewModel(MVVM) pattern provides a mechanism for binding action with a view. Command provides more control over the event in WPF, for example, you can put conditions, to check when your code will execute.

In programmatically we can say command is nothing but an implementation of the ICommand interface.

ICommand interface is part of .NET Framework, this interface is used a lot in MVVM applications. Mostly it is used to declare Command in MVVM.

The WPF ICommand interface is the code contract for commands that are written in .NET for Windows Runtime apps. These commands provide the commanding behavior for UI elements such as a Windows Runtime XAML Button and in particular an AppBarButton.

ICommand interface specifies three members:
  1. Execute():- The Execute method takes one parameter having datatype object and is called when the command is actuated. It has one parameter, which can be used to pass additional information from the caller to the command.
  2. CanExecute():- This method takes same parameter as Execute and return boolean,If the return value is true, it means that the command can be executed. The parameter is the same one as for the Execute method. When used in XAML controls that support the Command property, the control will be automatically disabled if CanExecute returns false.
  3. CanExecuteChanged :- It is an event handler and it must be raised by the command implementation when the CanExecute method needs to be reevaluated. In XAML, when an instance of ICommand is bound to a control’s Command property through a data-binding, raising the CanExecuteChanged event will automatically call the CanExecute method, and the control will be enabled or disabled accordingly.

The class that implements ICommand and have to implement the Execute and CanExecute method. But the CanExecuteChanged event does not need to be raised manually. A class named CommandManager is observing the user interface and calls the CanExecute method when it deems it necessary in Windows Presentation Framework(WPF).

Example:

 public class RelayCommand: ICommand   
 {   
   public boolCanExecute(object parameter)   
   {   
     // code to checking condition  
   }   
   public event EventHandlerCanExecuteChanged;   
   public void Execute(object parameter)  
   {   
     //Code for execution logic  
   }   

Note:-

XAML for Windows Runtime does not support x:Static, so don't attempt to use the x:Static markup extension if the command is used from Windows Runtime XAML. Also, the Windows Runtime does not have any predefined command libraries, so the XAML syntax shown here doesn't really apply for the case where you're implementing the interface and defining the command for Windows Runtime usage.

Need of Command

It's there to think that if event handling is there, so why the need for command in the .net framework. Previously .Net Framework was using event and event handler for notifying subscribers (instance that subscribes ) by the publisher(instance that exposes the event) when there is a need, but some problems were there with these approaches discussed below.

  1. Problem:- 1 Event handlers can create a tight coupling between the instance that exposes the event and the instance that subscribes to it. Because the system needs to keep track of event handlers so that they can be executed when the event is raised, but the strong link this creates might prevent garbage collection. Of course, this isn’t an issue if the event handler is a static method, but it is not always possible to handle all events with static methods only. This is a frequent cause of memory leaks in .NET.
  2. Problem:- 2 Because of tight coupling between an event and it's a handler, the event handler for a UI element declared in XAML must be found in the attached code-behind file. If it is not there, the compilation will fail with an error.

Command provides more control over the event in WPF, for example, you can put conditions, to check when your code will execute

Related Articles

  1. WPF Dispatcher
  2. WPF Data Validation: IDataErrorInfo
  3. Data Validation in WPF
  4. Resource in WPF

Summary

In this blog, we learned what is WPF Command, need of command and how to use WPF ICommand to create WPF command. I hope you have enjoyed it a lot.

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال