Skip to content

Instantly share code, notes, and snippets.

@UltraSabreman
Last active August 29, 2015 14:04
Show Gist options
  • Save UltraSabreman/ff5535d26188d70b858b to your computer and use it in GitHub Desktop.
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.
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