Skip to content

Instantly share code, notes, and snippets.

@Fireforge
Last active September 6, 2017 15:48
Show Gist options
  • Save Fireforge/149081c8913f70468f8d to your computer and use it in GitHub Desktop.
Save Fireforge/149081c8913f70468f8d to your computer and use it in GitHub Desktop.
Caliburn.Micro compliant PropChangedBase - https://dotnetfiddle.net/Fsiq61
// 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