Last active
January 21, 2022 02:39
-
-
Save ChuckSavage/814d5b9b61a251481ad3 to your computer and use it in GitHub Desktop.
Observe the changes in a value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ObservableValue<T> : INotifyPropertyChanged | |
{ | |
public class MyArgs : PropertyChangedEventArgs | |
{ | |
public MyArgs(string propertyName, T old, T @new) | |
: base(propertyName) | |
{ | |
Old = old; | |
New = @new; | |
} | |
public T Old { get; private set; } | |
public T New { get; private set; } | |
} | |
public event PropertyChangedEventHandler PropertyChanged = delegate { }; | |
private void NotifyValueChanged(T @new, T old) | |
{ | |
var EP = PropertyChanged; | |
EP(this, new MyArgs("Value", old, @new)); | |
} | |
public T Value | |
{ | |
get { return _Value; } | |
set | |
{ | |
T old = value; | |
SetValueSilently(value); | |
NotifyValueChanged(value, old); | |
} | |
} | |
T _Value; | |
public void SetValueSilently(T value) | |
{ | |
_Value = value; | |
} | |
public static implicit operator T(ObservableValue<T> observable) | |
{ | |
return observable.Value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment