using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; namespace NNTCPApp { /// /// Abstract Class create a collection of properties, and updates binding elements in view /// it's also used has a base class /// public abstract class ViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual bool PropertyHasChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged == null) return false; PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); return true; } /// /// This method is called by the Set accessor of each property. /// The CallerMemberName attribute that is applied to the optional propertyName /// parameter causes the property name of the caller to be substituted as an argument. /// /// /// /// protected void SetValue(T value, [CallerMemberName] String propertyName = "") { if (string.IsNullOrEmpty(propertyName)) return; _properties[propertyName] = value; PropertyHasChanged(propertyName); } /// /// This method is called by the Get accessor of each property /// The CallerMemberName attribute that is applied to the optional propertyName /// parameter causes the property name of the caller to be substituted as an argument. /// Returns stored property value /// /// /// /// protected T GetValue([CallerMemberName] String propertyName = "") { _properties.TryGetValue(propertyName, out object value); return (T)value; } //Store a collection of properties private Dictionary _properties { get; } = new Dictionary(); } }