Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#
Tip/Trick

Naming Properties in MVVM

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
20 Jul 2011CPOL1 min read 24.7K   1
A better method for naming properties in MVVM to solve some problems.

Some MVVM frameworks let you raise the PropertyChanged event by the property itself, not its “string” name. If you’re not using any of them, you may misspell them while typing their names as strings; and also, when you’re using them in more than one place, you’ll have some maintenance problems; e.g., if you rename a property, you have to change it in several places.


Keeping these problems in mind, I came up with this solution. If you think it’s not a good solution or you have better suggestions, they’re really welcomed ;)



  1. Add your view model class which implements INotifyPropertyChanged:
  2. C#
    class CustomersViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    }

  3. Add a new public method (e.g., RaisePropertyChanged) to centralize the code of raising the PropertyChanged event:
  4. C#
    public void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

  5. Add a private class to hold the names of properties inside the View Model class, like this:
  6. C#
    private class PropertyNames
    {
        public static string Id { get { return "Id"; } }
        public static string Name { get { return "Name"; } }
        public static string Email { get { return "Email"; } }
    }

    This class is private and cannot be accessed outside of your View Model; and also, you’re not going to create instances of it; that’s why all the properties are declared as static.


  7. Now you can add your notifiable properties to the View Model class like this:
  8. C#
    private int id;
    public int Id
    {
        get { return this.id; }
        set
        {
            if (this.id == value)
            {
                return;
            }
            this.id = value;
            RaisePropertyChanged(PropertyNames.Id);
        }
    }
    //add other properties here


As you can see, now you’re able to use the public properties of the PropertyNames class just to return their string names.


So the whole class will look like this:


C#
class CustomersViewModel : INotifyPropertyChanged
{
    private int id;
    public int Id
    {
        get { return this.id; }
        set
        {
            if (this.id == value)
            {
                return;
            }
            this.id = value;
            RaisePropertyChanged(PropertyNames.Id);
        }
    }
    //add other properties here
    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    private class PropertyNames
    {
        public static string Id { get { return "Id"; } }
        public static string Name { get { return "Name"; } }
        public static string Email { get { return "Email"; } }
    }
}

Please write your comments and opinions ;)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralHi Siavash, have you considered using a private enumeration ... Pin
Doc Lobster21-Jul-11 0:46
Doc Lobster21-Jul-11 0:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.