Return to Snippet

Revision: 42512
at March 5, 2011 22:31 by nimast


Initial Code
public class PresenterBase : INotifyPropertyChanged
    {
        private string _myProperty;
        public string MyProperty
        {
            get
            {
                return _myProperty;
            }
            set
            {
                _myProperty = value;
                OnPropertyChanged(() => MyProperty);
            }
        }

        protected virtual void OnPropertyChanged<T>(Expression<Func<T>> propertyEvaluator)
        {
            var lambda = propertyEvaluator as LambdaExpression;
            var member = lambda.Body as MemberExpression;
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(member.Member.Name));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

Initial URL

                                

Initial Description
The only way i know to create a normal "fire property changed" method that is still efficient yet strongly typed. The other common pattern uses run time checking and uses the TyepDescriptor for the current type to check for property existence - a much less elegant solution imho.

Initial Title
NotifyPropertyChanged

Initial Tags
c#

Initial Language
C#