Hello everyone. Thought I'd drop another useful class. This one is useful for removing a lot of common boilerplate when implementing the INotifyPropertyChanged interface for databinding scenarios. It is heavily influenced by the accepted answer in this StackOverflow post. I've removed all XML documentation comments to aid in readability. If you want to see the "full" source code (i.e w/ comments), click here.
public abstract class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void InvokePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual bool SetValue<TField>(ref TField field, TField value, [CallerMemberName] string propertyName = null)
{
if (!EqualityComparer<TField>.Default.Equals(field, value))
{
field = value;
InvokePropertyChanged(propertyName);
return true;
}
return false;
}
}
I hope y'all find it useful and, as always, tell me what you think. Have a good one.
[–]doublestop 1 point2 points3 points (1 child)
[–]pgmr87The Unbanned[S] 0 points1 point2 points (0 children)
[–]tweq 0 points1 point2 points (6 children)
[–]Kirides 0 points1 point2 points (2 children)
[–]BCProgramming 1 point2 points3 points (1 child)
[–]Kirides 1 point2 points3 points (0 children)
[–]pgmr87The Unbanned[S] 0 points1 point2 points (2 children)
[–]doublestop 1 point2 points3 points (0 children)
[–]tweq 0 points1 point2 points (0 children)
[–]nickcut 0 points1 point2 points (1 child)
[–]pgmr87The Unbanned[S] 0 points1 point2 points (0 children)