Skip to content

Instantly share code, notes, and snippets.

@dubeme
Created June 28, 2014 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dubeme/d7668aa8854d98c26451 to your computer and use it in GitHub Desktop.
Save dubeme/d7668aa8854d98c26451 to your computer and use it in GitHub Desktop.
Base class for data binding
/// <summary>
/// This class is the base for all classes that neds to be bound to
/// eg: this.set(ref this._FIELD, value);
/// </summary>
public class BindableClassBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Sets a property, then fires the PropertChanged Event
/// </summary>
/// <typeparam name="T">The type for the property being set.</typeparam>
/// <param name="storage">The field that will be storing the value</param>
/// <param name="value">The new value</param>
/// <param name="propertyName">THe name of the proprty being changed</param>
/// <returns>True if the value not equal the storage.</returns>
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
if (object.Equals(storage, value))
{
return false;
}
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
/// <summary>
/// Sets a property, then fires the PropertChanged Event.
/// This method doesn't care if the storage and value are the same.
/// </summary>
/// <typeparam name="T">The type for the property being set.</typeparam>
/// <param name="storage">The field that will be storing the value</param>
/// <param name="value">The new value</param>
/// <param name="propertyName">THe name of the proprty being changed</param>
protected void SetPropertyWithNoCheckForEquality<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
storage = value;
this.OnPropertyChanged(propertyName);
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var eventHandler = this.PropertyChanged;
if (eventHandler != null)
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment