Last active
September 6, 2017 15:48
-
-
Save Fireforge/149081c8913f70468f8d to your computer and use it in GitHub Desktop.
Caliburn.Micro compliant PropChangedBase - https://dotnetfiddle.net/Fsiq61
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
// C# 6.0 and up: | |
public class PropertyChangedBase : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged = delegate { }; | |
protected void NotifyOfPropertyChange(string name) | |
{ | |
PropertyChanged(this, new PropertyChangedEventArgs(name)); | |
} | |
} | |
// Minimum Binding Prop | |
public class MyClass : PropertyChangedBase | |
{ | |
private string _Name; | |
public string Name | |
{ | |
get { return _Name; } | |
set | |
{ | |
_Name = value; | |
NotifyOfPropertyChange(nameof(ValueTypedMember)); | |
} | |
} | |
} | |
// C# 5.0 and below: | |
public class PropertyChangedBase2 : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged = delegate {}; | |
protected void NotifyOfPropertyChange<T>(Expression<Func<T>> expr) | |
{ | |
if (expr == null) | |
throw new ArgumentNullException("expr"); | |
var member = expr.Body as MemberExpression; | |
var unary = expr.Body as UnaryExpression; | |
if (unary != null) | |
member = unary.Operand as MemberExpression; | |
if (member == null) | |
throw new ArgumentException("The body must be a member or unary expression"); | |
PropertyChanged(this, new PropertyChangedEventArgs(member.Member.Name)); | |
} | |
} | |
// Minimum Binding Prop | |
public class MyClass : PropertyChangedBase2 | |
{ | |
private string _Name; | |
public string Name | |
{ | |
get { return _Name; } | |
set | |
{ | |
_Name = value; | |
NotifyOfPropertyChange(() => ValueTypedMember); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment