Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't anyone use the INotifyPropertyChanging?

Tags:

c#

mvvm

wpf

I know MVVM heavily uses the INotifyPropertyChanged, but I have never seen any usage of the INotifyPropertyChanging. Any reason why?

If I did want to use this, what would be a good way to integrate this into my MVVM Framework? I know you're not supposed to use MessageBox on your ViewModel because then you can't unit test it. So how would one go about throwing up an alert, then continuing on with the PropertyChange if applicable?

like image 209
michael Avatar asked Nov 03 '11 13:11

michael


People also ask

What is the purpose of INotifyPropertyChanged?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

Does Blazor use INotifyPropertyChanged?

Does Blazor use INotifyPropertyChanged? Syncfusion Blazor components supports to automatically update data based on INotifyCollectionChanged ( ObservableCollection ) and INotifyPropertyChanged interfaces.

Does ObservableCollection implement INotifyPropertyChanged?

INotifyCollectionChanged . ObservableCollection implements INotifyCollectionChanged which will notify us when the collection has a mutated state.

How do you implement INotifyPropertyChanged?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.


1 Answers

Something to keep in mind about INotifyPropertyChanging is you can't stop the change from happening. This merely allows you to record that the change occurred.

I use it in a framework of mine for change tracking, but it isn't an appropriate method for halting changes.

You could extend your ViewModelBase with a custom interface/event pair:

delegate void AcceptPendingChangeHandler(
    object sender,
    AcceptPendingChangeEventArgs e);

interface IAcceptPendingChange
{
    AcceptPendingChangeHandler PendingChange;
}

class AcceptPendingChangeEventArgs : EventArgs
{
    public string PropertyName { get; private set; }
    public object NewValue { get; private set; }
    public bool CancelPendingChange { get; set; }
    // flesh this puppy out
}

class ViewModelBase : IAcceptPendingChange, ...
{
    protected virtual bool RaiseAcceptPendingChange(
        string propertyName,
        object newValue)
    {
        var e = new AcceptPendingChangeEventArgs(propertyName, newValue)
        var handler = this.PendingChange;
        if (null != handler)
        {
            handler(this, e);
        }

        return !e.CancelPendingChange;
    }
}

At this point you'd need to add it by convention to your view models:

class SomeViewModel : ViewModelBase
{
     public string Foo
     {
         get { return this.foo; }
         set
         {
             if (this.RaiseAcceptPendingChange("Foo", value))
             {
                 this.RaiseNotifyPropertyChanging("Foo");
                 this.foo = value;
                 this.RaiseNotifyPropretyChanged("Foo");
             }
         }
     }
}
like image 112
user7116 Avatar answered Sep 22 '22 14:09

user7116