using System; using System.Collections.Generic; using System.ComponentModel; using System.Reactive.Linq; using System.Runtime.CompilerServices; using System.Text; namespace AIStudio.Wpf.DiagramDesigner { /// /// Implementation of to simplify models. /// public abstract class BindableBase : INotifyPropertyChanged { /// /// Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; private Dictionary _oldDic = new Dictionary(); public void SetOldValue(T value, string propertyName, string guid = null) { _oldDic[propertyName + guid ?? string.Empty] = value; } public T GetOldValue(string propertyName, string guid = null) { if (_oldDic.ContainsKey(propertyName + guid ?? string.Empty)) { var old = (T)_oldDic[propertyName + guid ?? string.Empty]; return old; } else { return default(T); } } public void ClearOldValue(string propertyName, string guid = null) { if (_oldDic.ContainsKey(propertyName + guid ?? string.Empty)) { _oldDic.Remove(propertyName + guid ?? string.Empty); } } /// /// Checks if a property already matches a desired value. Sets the property and /// notifies listeners only when necessary. /// /// Type of the property. /// Reference to a property with both getter and setter. /// Desired value for the property. /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers that /// support CallerMemberName. /// True if the value was changed, false if the existing value matched the /// desired value. protected virtual bool SetProperty(ref T storage, T value, [CallerMemberName] string propertyName = null) { if (EqualityComparer.Default.Equals(storage, value)) return false; if (propertyName == "IsSelected") { } else if (this.ContainsProperty("IsReadOnly")) { if (object.Equals(this.GetPropertyValue("IsReadOnly"), true)) return false; } var old = storage; storage = value; RaisePropertyChanged(propertyName, old, value); return true; } /// /// Checks if a property already matches a desired value. Sets the property and /// notifies listeners only when necessary. /// /// Type of the property. /// Reference to a property with both getter and setter. /// Desired value for the property. /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers that /// support CallerMemberName. /// Action that is called after the property value has been changed. /// True if the value was changed, false if the existing value matched the /// desired value. protected virtual bool SetProperty(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null) { if (EqualityComparer.Default.Equals(storage, value)) return false; if (propertyName == "IsSelected") { } else if (this.ContainsProperty("IsReadOnly")) { if (object.Equals(this.GetPropertyValue("IsReadOnly"), true)) return false; } var old = storage; storage = value; onChanged?.Invoke(); RaisePropertyChanged(propertyName, old, value); return true; } /// /// Raises this object's PropertyChanged event. /// /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers /// that support . protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } protected void RaisePropertyChanged([CallerMemberName] string propertyName = null, object oldvalue = null, object newvalue = null) { OnPropertyChanged(new ValuePropertyChangedEventArgs(propertyName, oldvalue, newvalue)); } protected void RaisePropertyChanged(object sender, [CallerMemberName] string propertyName = null, object oldvalue = null, object newvalue = null) { PropertyChanged?.Invoke(sender, new ValuePropertyChangedEventArgs(propertyName, oldvalue, newvalue)); } protected void RaisePropertyChanged(object sender, PropertyChangedEventArgs args) { PropertyChanged?.Invoke(sender, args); } /// /// Raises this object's PropertyChanged event. /// /// The PropertyChangedEventArgs protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) { PropertyChanged?.Invoke(this, args); } public IObservable WhenPropertyChanged { get { return Observable .FromEventPattern( h => this.PropertyChanged += h, h => this.PropertyChanged -= h) .Select(x => x.EventArgs.PropertyName); } } } public class ValuePropertyChangedEventArgs : PropertyChangedEventArgs { public ValuePropertyChangedEventArgs(string propertyName, object oldValue, object newValue) : base(propertyName) { OldValue = oldValue; NewValue = newValue; } public object OldValue { get; set; } public object NewValue { get; set; } } }