Created
February 16, 2009 21:24
-
-
Save cbilson/65379 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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; } | |
| } | |
| } | |
| } |
This file contains hidden or 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
| 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