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?
The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.
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)
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.
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.
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 ).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With