14

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.

Malu MN
  • 141
  • 2
  • 10
ChiliYago
  • 11,341
  • 23
  • 79
  • 126
  • 1
    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? – BradleyDotNET Mar 04 '15 at 00:17
  • I use `BindableBase` in my Prism projects. It's pretty lustrous still :) – dub stylee Mar 04 '15 at 00:25

3 Answers3

30

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));
      }
    }
}
Rohit
  • 10,056
  • 7
  • 50
  • 82
7

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.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • So BindableBase is exclusively for Prism? If I wanted to use Prism and standard MVVM I should just stick with the INotifyPropertyChanged approach. – ChiliYago Mar 04 '15 at 21:40
  • 1
    @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. – Emond Mar 05 '15 at 04:55
  • "It is not a choice between these two." That's a misleading statement. It's not a bidirectional dependency. You can choose to only implement INotifyPropertyChanged without BindableBase. It's a choice of whether you want the implementation of BindableBase or not. Saying there's not a choice disregards that distinction. – AaronLS Jan 25 '22 at 19:33
3

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.

  • Which means BindableBase has little to no advantage over plain INotifyChanged. Moreover if you already have a base class you can't use BindableBase. – Slion Jan 26 '21 at 14:29
  • `Moreover if you already have a base class you can't use BindableBase.` Your base class *can* inherit `BindableBase`. All `BindableBase` does is implement INPC. The question then become: Do I want to use someone else implementation of INPC, or do i want to write my own. If your base class already implements INPC, then you've chose to write your own. The only advantage is someone else already did it for you (less boilerplate). What *advantage* were you expecting? – highboi Feb 25 '21 at 16:30