The .NET Command Pattern, Part 2: Listing 2

The Execute method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
 
namespace VSMCommandPatternUndo.Commands
{
    public class UndoCommand : ICommand
    {
        public UndoCommand()
        {
            CommandStateManager.Instance.PropertyChanged += new
System.ComponentModel.PropertyChangedEventHandler(Instance_PropertyChanged); } void Instance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { CanExecuteChanged(this, e); } public bool CanExecute(object parameter) { return CommandStateManager.Instance.CanUndo; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { CommandStateManager.Instance.Undo(); } } }

About the Author

Eric Vogel is a Sr. Software Developer at Kunz, Leigh, & Associates in Okemos, MI. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at vogelvision@gmail.com.