using System.ComponentModel; namespace Apex.MVVM { /// <summary> /// Standard viewmodel class base, simply allows property change notifications to be sent. /// </summary> public class ViewModel : INotifyPropertyChanged { /// <summary> /// The property changed event. /// </summary> public event PropertyChangedEventHandler PropertyChanged; /// <summary> /// Raises the property changed event. /// </summary> /// <param name="propertyName">Name of the property.</param> public virtual void NotifyPropertyChanged(string propertyName) { // If the event has been subscribed to, fire it. if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } /// <summary> /// Gets the value of a notifying property. /// </summary> /// <param name="notifyingProperty">The notifying property.</param> /// <returns>The value of the notifying property.</returns> protected object GetValue(NotifyingProperty notifyingProperty) { return notifyingProperty.Value; } /// <summary> /// Sets the value of the notifying property. /// </summary> /// <param name="notifyingProperty">The notifying property.</param> /// <param name="value">The value to set.</param> /// <param name="forceUpdate">If set to <c>true</c> we'll force an update /// of the binding by calling NotifyPropertyChanged.</param> protected void SetValue(NotifyingProperty notifyingProperty, object value, bool forceUpdate = false) { // We'll only set the value and notify that it has changed if the // value is different - or if we are forcing an update. if (notifyingProperty.Value != value || forceUpdate) { // Set the value. notifyingProperty.Value = value; // Notify that the property has changed. NotifyPropertyChanged(notifyingProperty.Name); } } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)