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

Naming Properties in MVVM

By , 20 Jul 2011
 

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

License

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

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralHi Siavash, have you considered using a private enumeration ...memberDoc Lobster21 Jul '11 - 0:46 

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

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