BindableBase vs INotifyChanged

inotifypropertychanged
prism mvvm
propertychanged invoke
prism mvvm wpf example
c# inotifypropertychanged automatic properties
inotifypropertychanged attribute
inotifypropertychanged c# 7
prism bind view to viewmodel

Does anyone know if BindableBase is still a viable or should we stick with INotifyChanged events? It seems like BindableBase has lost its luster quickly. Thanks for any info you can provide.

INotifyPropertyChanged

The ViewModel should implement the INotifyPropertyChanged interface and should raise it whenever the propertychanges

public class MyViewModel : INotifyPropertyChanged
{
    private string _firstName;


    public event PropertyChangedEventHandler PropertyChanged;

    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName == value)
                return;

            _firstName = value;
            PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
        }
    }


    }
}

Problem is with ICommand interface as most of the code is duplicated also since it passes string it becomes error prone.

Whereas Bindablebase is an abstract class that implements INotifyPropertyChanged interface and provide SetProperty<T>.You can reduce the set method to just one line also ref parameter allows you to update its value. The BindableBase code below comes from INotifyPropertyChanged, The .NET 4.5 Way - Revisited

   public class MyViewModel : BindableBase
{
    private string _firstName;
    private string _lastName;

    public string FirstName
    {
        get { return _firstName; }
        set { SetProperty(ref _firstName, value); }
    }


}

     //Inside Bindable Base
    public abstract class BindableBase : INotifyPropertyChanged
    {

       public event PropertyChangedEventHandler PropertyChanged;

       protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
       {
          if (Equals(storage, value))
          {
             return false;
          }

          storage = value;
          this.OnPropertyChanged(propertyName);
          return true;
       }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
      PropertyChangedEventHandler eventHandler = this.PropertyChanged;
      if (eventHandler != null)
      {
          eventHandler(this, new PropertyChangedEventArgs(propertyName));
      }
    }
}

BindableBase | WPF Controls, Implementation of INotifyPropertyChanged to simplify models. Namespace: public abstract class BindableBase : INotifyPropertyChanged Whereas Bindablebase is an abstract class that implements INotifyPropertyChanged interface and provide SetProperty<T>.You can reduce the set method to just one line also ref parameter allows you to update its value

It is not a choice between these two.

BindableBase implements INotifyPropertyChanged.

So if you use BindableBase you will be using INotifyPropertyChanged.

INotifyPropertyChanged is more or less mandatory when implementing MVVM using DataBinding.

Whether to use BindableBase or an other implementation depends on preference and use of Prism.

BindableBase Class (Microsoft.Practices.Prism.Mvvm), Implementing the INotifyPropertyChanged interface in your view model or model The Prism Library provides the BindableBase base class from which you can  BindableBase vs INotifyChanged ¿Alguien sabe si BindableBase es todavía viable o debemos palo con INotifyChanged eventos? Parece que BindableBase ha perdido su brillo rápidamente.

To expand on Rohit's answer, if you are using .NET 4.6 you can take advantage of the Null-conditional operator and simplify the OnPropertyChanged method in the following way:

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

INotifyPropertyChanged, The .NET 4.6 Way explains it in more detail.

Implementing the MVVM Pattern Using the Prism Library for WPF , By implementing ​INotifyPropertyChanged​, we can notify changes in the two classes: ​BindableBase​ and ComputedBindableBase​. The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider an Employee object with a property called Name . To provide generic property-change notification, the Employee type implements the INotifyPropertyChanged interface and raises a PropertyChanged event

MVVM Base in C# - Making Tuenti, code by creating a BindableBase class that implements INotifyPropertyChanged. then you can subclass BindableBase in your models or view models to make  Implementing INotifyPropertyChanged manually is not hard. Newer versions of C# provide us with CallerMemberName attribute and calls to method to fire event for property changes get even smaller. Plus we don’t have to write property names as strings.

Xamarin Mobile Application Development: Cross-Platform C# and , The method SetProperty is defined by the base class BindableBase (code file elements needs either dependency properties or INotifyPropertyChanged to  Examples. The following code example demonstrates the how to implement the INotifyPropertyChanged interface. When you run this example, you will notice the bound DataGridView control reflects the change in the data source without requiring the binding to be reset.

Professional C# 7 and .NET Core 2.0, In the file BindableBase.cs, the class BindableBase is defined. This class implements the interface INotifyPropertyChanged and offers the method SetProperty  INotifyPropertyChanged interface is used to notify the view or ViewModel that it does not matter which property is binding; it is updated. Let's take an example for understanding this interface.

Comments
  • For starters, BindableBase is a Prism thing, so isn't relevant to vanilla WPF (though the concept could be). Could you be more specific in what you are asking?
  • I use BindableBase in my Prism projects. It's pretty lustrous still :)
  • So BindableBase is exclusively for Prism? If I wanted to use Prism and standard MVVM I should just stick with the INotifyPropertyChanged approach.
  • @ChiliYago any answer on that is pure opinion; it's up to you to decide. There is not standard MVVM, it is just as set of guidelines that help you assign responsibilities to classes. What implementation you use is not in the pattern.