Skip to content

Instantly share code, notes, and snippets.

@Deathspike
Created August 24, 2015 08:30
Show Gist options
  • Save Deathspike/58258e71ce6d14cf3d76 to your computer and use it in GitHub Desktop.
Save Deathspike/58258e71ce6d14cf3d76 to your computer and use it in GitHub Desktop.
Example of a Bindable in .NET 4.5
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Rs.Os
{
internal abstract class Bindable : IBindable
{
#region Abstract
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected void OnPropertyChanging(string propertyName)
{
var handler = PropertyChanging;
if (handler != null) handler(this, new PropertyChangingEventArgs(propertyName));
}
protected bool Set<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
OnPropertyChanging(propertyName);
field = value;
OnPropertyChanged(propertyName);
return true;
}
#endregion
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
#endregion Implementation of INotifyPropertyChanged
#region Implementation of INotifyPropertyChanging
public event PropertyChangingEventHandler PropertyChanging;
#endregion Implementation of INotifyPropertyChanging
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment