Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Naming Properties in MVVM

, 20 Jul 2011
Rate this:
Please Sign up or sign in to vote.
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 Wink | ;)

  1. Add your view model class which implements INotifyPropertyChanged:
  2. 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. 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. 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. 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:

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 Wink | ;)

License

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

Share

About the Author

Siavash Mortazavi
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 20 Jul 2011
Article Copyright 2011 by Siavash Mortazavi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid