Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the importance of RaisePropertyChanged?

I am currently learning MVVM and i don't understand how RaisePropertyChanged is important

For example this function :

public City SelectedCity
{
    get
    {
        return selectedcity;
    }
    set
    {
        RaisePropertyChanging(SelectedCityLocationPropertyName);
        selectedtrend = value;
        RaisePropertyChanged(SelectedCityLocationPropertyName);
        MessageBox.Show(City.Name);
    }
}

and this one :

public City SelectedCity
{
    get
    {
        return selectedcity;
    }
    set
    {
        //   RaisePropertyChanging(SelectedCityLocationPropertyName);
        selectedtrend = value;
        //   RaisePropertyChanged(SelectedCityLocationPropertyName);
        MessageBox.Show(City.Name);
    }
}

Give exactly the same result for me. Can you please tell me why is RaisePropertyChanged so important and give me an example where it would make a vital difference?

like image 703
user2505650 Avatar asked Jul 03 '14 19:07

user2505650


People also ask

What is the use of INotifyPropertyChanged in C#?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.

What is RaisePropertyChanged WPF?

RaisePropertyChanged("User"); From MSDN: The PropertyChanged event can indicate all properties on the object have changed by using either null or String. Empty as the property name in the PropertyChangedEventArgs. (No need to refresh all the Properties in this case)


3 Answers

Read this:

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

The RaisePropertyChanging event is used to notify UI or bound elements that the data has changed. For example a TextBox needs to receive a notification when the underlying data changes, so that it can update the text you see in the UI. Without the NotifyPropertyChanged event, the TextBox would have no idea that the data changed.

It's very important in MVVM.

like image 80
Jon Avatar answered Oct 16 '22 23:10

Jon


The RaisePropertyChange are events which signal a change in status of the property to those who subscribe to the class. If you look at the base class in MVVM light you will find that it adheres to INotifyPropertyChanged.

When a property notifies a change a subscriber (most likely a binding in the Xaml), the consumer of the event knows to update the control with new data. That allows the view to be updated asynchronously without having to directly update any bound control(s).

See my answer to MVVM update of calculated properties for an example where updates can be pushed using the INotifyPropertyChange.

Also on my blog I discuss MVVM binding (which MVVM light simply is a wrapper for) Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding. which may show you how its done without the MVVM light helper wrappers.

like image 25
ΩmegaMan Avatar answered Oct 17 '22 00:10

ΩmegaMan


WPF binding mechanism relies on the DataContext of each FrameworkElement to Raise PropertyChanged event in order for it's Dependency Properties in that to sample the value of the plain CLR property they are bound to.

Dependency Property <- Binding -> Plain CLR Property

When loaded each of the FrameworkElement's Dependency Properties will be given the value from the bound CLR Property.

The Binding engine listens to the PropertyChanged event , When raised it locates the corresponding Dependency Property (Properties) bound the CLR Property which name is given in the event args , and updates their value from it ( at this point you would reach your CLR Property's getter ).

like image 33
eran otzap Avatar answered Oct 17 '22 00:10

eran otzap