Last active
August 29, 2015 14:04
-
-
Save UltraSabreman/ff5535d26188d70b858b to your computer and use it in GitHub Desktop.
Allows you to make properties execute the PropertyChanged event, with less ugly code.
This file contains 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
public class ViewModelNotifier : INotifyPropertyChanged { | |
private Dictionary<String, Object> propertyValues = new Dictionary<String, Object>(); | |
public event PropertyChangedEventHandler PropertyChanged; | |
protected void SetProp<T>(T value, [CallerMemberName] string property = null) { | |
propertyValues[property] = value; | |
OnPropertyChanged(property); | |
} | |
protected T GetProp<T>([CallerMemberName] string property = null) { | |
try { | |
return (T)propertyValues[property]; | |
} catch (Exception) { | |
return default(T); | |
} | |
} | |
protected void OnPropertyChanged(string propertyName) { | |
PropertyChangedEventHandler handler = PropertyChanged; | |
if (handler != null) | |
handler(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
} | |
/* Example Usage: | |
class model : ViewModelNotifier { | |
public String TextBoxText { get { return GetProp<String>(); } set { SetProp(value); } } | |
}*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment