Add code to ViewModel Base to take advantage of Intellisense

2 replies. Last post: July 7, 2010 7:55 AM by Bob Baker
Tags :
  • New Discussion
  • New Question
  • New Product Feedback
Bob BakerMember

Adding something like this to your ViewModelBase:

        public void RaisePropertyChanged<TProperty>(Expression<Func<TProperty>> property)
        {
            if (PropertyChanged == null)
            {
                return;
            }

            var lambda = (LambdaExpression)property;

            MemberExpression memberExpression;

            if (lambda.Body is UnaryExpression)
            {
                var unaryExpression = (UnaryExpression)lambda.Body;
                memberExpression = (MemberExpression)unaryExpression.Operand;
            }
            else memberExpression = (MemberExpression)lambda.Body;

            OnPropertyChanged(memberExpression.Member.Name);
        }

will allow for code like this in ViewModel class property setters:

RaisePropertyChanged( () => MyProperty);

The result is no more typos inside of string properties. Of course, if you've copying and pasting, you can still forget to change the property for the pasted one.

 

All times are GMT -5. The time now is 12:17 AM.
Previous Next