MVVM – Environment – MGViewModel

In the MVVM pattern I always need the same base structure of the ViewModel.

So I decided to write my own class which could be extended when creating a new ViewModel.
Such that I do not have to implement the same features each time.

This base class implements INotifyPropertyChanged and a few methods which makes it easy to fire this eventt when one of the properties changed.

The NotifyPropertyChanged-Method which takes the propertyExpression parameter is by Eric De Caroufel

public  class MGViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Member

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Util Methods

        /// <summary>
        /// This method fires the PropertyChanged-Event
        /// </summary>
        /// <param name="propertyExpression">The expression which contains the variable which was changed</param>
        protected void NotifyPropertyChanged(Expression<Func<object>> propertyExpression)
        {
            var propertyChanged = PropertyChanged;
            if (propertyChanged != null)
            {
                // Retreive lambda body
                var body = propertyExpression.Body as MemberExpression;
                if (body == null)
                    throw new ArgumentException("'propertyExpression' should be a member expression");

                // Extract the right part (after "=>")
                var vmExpression = body.Expression as ConstantExpression;
                if (vmExpression == null)
                    throw new ArgumentException("'propertyExpression' body should be a constant expression");

                // Create a reference to the calling object to pass it as the sender
                LambdaExpression vmlambda = Expression.Lambda(vmExpression);
                Delegate vmFunc = vmlambda.Compile();
                object vm = vmFunc.DynamicInvoke();

                // Extract the name of the property to raise a change on
                string propertyName = body.Member.Name;
                var e = new PropertyChangedEventArgs(propertyName);
                propertyChanged(vm, e);
            }
        }

        /// <summary>
        /// This method fires the PropertyChanged-Event
        /// </summary>
        /// <param name="name">The name of the variable which was changed</param>
        protected void NotifyPropertyChanged(string name)
        {
            if(PropertyChanged!=null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        
        #endregion
    }
Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: