using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace mdfinder { /// A class responsible for implementing the interface and helper functions. public abstract class PropertyChangedAlerter : INotifyPropertyChanged { #region Members /// Occurs when a property value changes. public event PropertyChangedEventHandler PropertyChanged; /// A list of properties to always call as updated. Generally used for composite properties. private List alwaysCall = new List(); #endregion #region Properties // #endregion #region Methods /// /// Executes the property changed action. This alerts subscribers to its change in value. /// /// (Optional) The name of the property. /// /// This will automatically pass in "SomeProperty" as the property name, derived useing the /// attribute. /// /// public bool SomeProperty /// { /// get /// { /// return this.someProperty; /// } /// set /// { /// this.someProperty = value; /// OnPropertyChanged(); /// } /// } /// /// protected virtual void OnPropertyChanged([CallerMemberName]string name = null) { if (!string.IsNullOrWhiteSpace(name)) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); foreach (var updatedProperty in this.alwaysCall) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(updatedProperty)); } } } /// Executes when all properties are changed and should be updated. protected virtual void OnAllPropertiesChanged() { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(string.Empty)); } /// Adds a property that will always be called when any property is updated.. /// The name of the property. public void AddConstantCallProperty(string name) { if (this.alwaysCall == null) { // This item has been deserialized and the list needs to be reinitialized. this.alwaysCall = new List(); } if (!this.alwaysCall.Any(c => c.Equals(name, StringComparison.OrdinalIgnoreCase))) { this.alwaysCall.Add(name); } } #endregion } }