Click here to Skip to main content
15,881,803 members
Articles / Desktop Programming / Universal Windows Platform
Tip/Trick

Self Evaluating Command for MVVM Pattern

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
14 Jan 2018CPOL1 min read 5.8K   36   1   1
ActionCommand variant that tracks changes of properties and raises CanExecuteChange event on its own.

Introduction

For quite some time, I’ve been using the common ActionCommand implementation that can be found in almost every MVVM library. It works very well in MVVM world, but I didn’t really like all those “RaiseCanExecuteChanged” calls scattered all around my ViewModels code. So I decided to turn things around and make the command responsible for tracking property changes. Hopefully, someone will find this useful.

TLDR: How Do I Use It?

Just instantiate it like a normal ActionCommand, passing Execute and CanExecute callbacks. And register all properties that should be tracked. Do note that properties must be defined on an object implementing INotifyPropertyChange interface.

C#
private SelfEvaluatingActionCommand increaseCounter;

public ICommand IncreaseCounter
{
    get
    {
        if (increaseCounter == null)
        {
            increaseCounter = new SelfEvaluatingActionCommand(OnIncreaseCounter, CanIncreaseCounter)
                .AddPropertyTrigger(() => CounterMax)
                .AddPropertyTrigger(() => Counter);
        }

        return increaseCounter;
    }
}

And you are all set to go. Whenever CounterMax or Counter property changes, command will fire its CanExecuteChange event.

Description and Extendibility

Have a look at the attached file which contains all necessary classes along with a basic sample project.

The core logic lies in PropertyTracker class, which takes care of resolving property names provided via expression and hooking up to property change events of source objects. Internally, it builds a dictionary of source notification objects along with property names registered per given object. Whenever registered property changes, PropertyTracker will invoke an action that was passed to it via constructor.

This way, you can hook up property tracking to your own commands without being restricted by existing inheritance chain. Of course, you can still subclass SelfEvaluatingActionCommand if desired.

When implementing on your own set of commands via composition, be sure to implement ISelfEvaluatingCommand interface. This will let you use those “Fluent” extension methods found in CommandExtensions class.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Slovakia Slovakia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice, but be careful Pin
Martin Lottering19-Feb-18 1:27
Martin Lottering19-Feb-18 1:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.