dimanche 26 mai 2013

MVVM: Intellisense for getting property name - Intellisense afin de retrouver le nom d'une propriete

By using MVVM,

We all know how boring is to write each name of property in the setter. Mistake, Mispelling lead to debugging.

Here is a small extension methond using linq in order to solve this problem:


Code:
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Collections.ObjectModel;
using System.Collections;
using System.Collections.Generic;
using System.Linq;


namespace Refre.Utils
{
    public static class Helper
    {
        public static void Raise<T, P>(this PropertyChangedEventHandler pc, T source, Expression<Func<T, P>> pe)
        {
            if (pc != null)
                pc.Invoke(source, new PropertyChangedEventArgs(((MemberExpression)pe.Body).Member.Name));
        }
    }
}

Now, we can easily use intellisense in order to find property while writting the setter. here is an example:
Code:
 private string btnName;
        /// <summary>
        /// Gets or sets the button drink name.
        /// </summary>
        public string BtnName
        {
            get { return btnName; }
            set 
            {
                if (!object.Equals(btnName, value))
                {
                    btnName = value;
                    PropertyChanged.Raise(this, o => o.BtnName);
                }
            }
        }
Good work

Aucun commentaire:

Enregistrer un commentaire