Created
August 24, 2015 08:30
-
-
Save Deathspike/58258e71ce6d14cf3d76 to your computer and use it in GitHub Desktop.
Example of a Bindable in .NET 4.5
This file contains hidden or 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
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