Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created February 16, 2009 21:24
Show Gist options
  • Save cbilson/65379 to your computer and use it in GitHub Desktop.
Save cbilson/65379 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.ComponentModel;
using WpfApplication1.Models;
namespace WpfApplication1.ViewModels
{
public class PersonEditViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (s, e) => { };
private Person _person = new Person();
public string FirstName {
get { return _person.FirstName; }
set {
_person.FirstName = value;
PropertyChanged.Notify(() => FirstName);
PropertyChanged.Notify(() => DisplayName);
}
}
public string MiddleName {
get { return _person.MiddleName; }
set {
_person.MiddleName = value;
PropertyChanged.Notify(() => MiddleName);
PropertyChanged.Notify(() => DisplayName);
}
}
public string LastName {
get { return _person.LastName; }
set {
_person.LastName = value;
PropertyChanged.Notify(() => LastName);
PropertyChanged.Notify(() => DisplayName);
}
}
public string DisplayName {
get { return _person.DisplayName; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
using System.ComponentModel;
namespace WpfApplication1.ViewModels
{
public static class Helpers
{
public static void Notify(this PropertyChangedEventHandler eventHandler, Expression<Func<object>> expression) {
if (null == eventHandler) {
return;
}
var lambda = expression as LambdaExpression;
MemberExpression memberExpression;
if (lambda.Body is UnaryExpression) {
var unaryExpression = lambda.Body as UnaryExpression;
memberExpression = unaryExpression.Operand as MemberExpression;
} else {
memberExpression = lambda.Body as MemberExpression;
}
var constantExpression = memberExpression.Expression as ConstantExpression;
var propertyInfo = memberExpression.Member as PropertyInfo;
foreach (PropertyChangedEventHandler del in eventHandler.GetInvocationList()) {
del(constantExpression.Value, new PropertyChangedEventArgs(propertyInfo.Name));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment