Skip to content

Instantly share code, notes, and snippets.

@ChuckSavage
Last active January 21, 2022 02:39
Show Gist options
  • Save ChuckSavage/814d5b9b61a251481ad3 to your computer and use it in GitHub Desktop.
Save ChuckSavage/814d5b9b61a251481ad3 to your computer and use it in GitHub Desktop.
Observe the changes in a value
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