[MVVMbasics] New version 2.0 features #2: Simplified Data Binding with more flexibility

[notice]All blog posts with a title that starts with [MVVMbasics] are related to the MVVMbasics framework. If you are only interested in my regular blog posts and not in MVVMbasics, ignore these. To list all articles covering MVVMbasics, go to http://mvvmbasics.mobilemotion.eu/documentation![/notice]

Data Binding: Simpler and more flexible

As we know, all Viewmodel properties that shall allow Data Binding need to be implemented in a special way in order to raise the PropertyChanged event when their value is updated. MVVMbasics introduced the Set method that significantly reduces those properties’ Setters. That’s what we already know from version 1.9, but let’s take a look at how this Set method has changed in the 2.0 version:

Simple property Setter:

In its simplest form, the Set method does the following:

  1. check if the property’s new value differs from the old one
  2. if yes, replace the old value with the new one
  3. and fire the PropertyChanged event

In all the 1.x versions, this Set method had the following signature:

Set(ref _propertyName, value, () => PropertyName);

Thanks to .NET 4.5 and a few adaptations in the MVVMbasics framework, we can now reduce this to:

Set(ref _propertyName, value);

Which is already a bit shorter and easier to write! An example of a full bindable property specification could look like the following:

private string _someText;
public string SomeText
{
	get { return _someText; }
	set { Set(ref _someText, value); }
}

Additional methods within the Setter:

It has always been possible to add an additional task to the Set method: An additional method that is called automatically after the property’s value has been updated, but only if the old and new values were different. This is quite useful to, for example, fire the PropertyChanged event for additional properties that depend on the current one.

This is still possible, but – of course – using the shorter Set method as presented above:

private string _someText;
public string SomeText
{
	get { return _someText; }
	set { Set(ref _someText, value, AdditionalMethod); }
}

private void AdditionalMethod()
{
	// do whatever is appropriate after the SomeText property has been updated
}

Version 2.0 allows you to specify an additional method that is called before the property’s value is updated (but only if the old and new values are different) – this one returns the type bool to express if the Set method should continue its work with updating the property and firing the PropertyChanged event, or not! This additional method (it’s called before in the Set method’s signature can be specified as the first parameter:

private string _someText;
public string SomeText
{
	get { return _someText; }
	set { Set(CheckIfUpdate, ref _someText, value); }
}

private bool CheckIfUpdate()
{
	// return either TRUE to continue with updating the property and
	// firing the <code>PropertyChanged</code> event, or FALSE to abort
}

And, of course it’s possible to specify both the before and the after method:

private string _someText;
public string SomeText
{
	get { return _someText; }
	set { Set(CheckIfUpdate, ref _someText, value, AdditionalMethod); }
}

private bool CheckIfUpdate()
{
	// return either TRUE to continue with updating the property and
	// firing the <code>PropertyChanged</code> event, or FALSE to abort
}

private void AdditionalMethod()
{
	// do whatever is appropriate after the SomeText property has been updated
}

Dependent Commands:

In all previous versions, it has been possible to specify all Commands that have a CanExecute condition which depends on the current property’s value, as part of the Set method. Due to a really cool new feature of MVVMbasics 2.0, this is not necessary any more – Commands can do this on their own (as you will learn in Part #3 of this series of articles)! This means: The Set method stays as clean as it appears in the above examples, no more parameters to add.

A note about backwards compatibility:

The “old” versions of the Set method are still available, but are flagged as deprecated and will be removed from the framework in a future version. When creating a new project, never use deprecated functions but only the new ones. When migrating an existing project to MVVMbasics 2.0, you can still use the old methods, but remember to plan an upgrade task to replace these at a later point in time. It’s not that complex, and it guarantees that you’re project is future-proof. (By the way, Visual Studio will not let you forget about that since it shows a compiler warning for each deprecated function that is still in use…)

Alright, let’s move on and learn how much easier is has become to define custom commands in Part #3: Intelligent Command Binding!