Click here to Skip to main content
11,796,774 members (79,639 online)
Click here to Skip to main content

Tagged as

Safest way to use RaisePropertyChanged method

, 14 Nov 2013 CPOL 17.2K 228 11
Rate this:
Please Sign up or sign in to vote.
Safest way to use RaisePropertyChanged method.


In my previous post on Simplest MVVM Ever, I gave the simplest overview of implementing MVVM. Now moving forward, I thought to extend my previous post by picking individual areas. And this time, I selected RaisePropertyChanged. All the developers working on XAML related apps are very well aware about the use of this RaisePropertyChanged. I am sure, most of us are also aware on where to use this. But does everyone aware about what is the proper way or let's say generic way to use it ??? Well, even if you are not aware, no problem. At the end of this post, you will surely take home a useful tip on using RaisePropertyChanged.


As we know that MVVM provides a loose coupling methodology. At lot many places it is very useful to get benefit of such architecture, but at the cost of your alertness. Because when we are talking about MVVM, we usually says that ViewModel has no knowledge about View and properties are the way, who binds View and ViewModel to show the data on the UI. Here is the gotcha !!!

ViewModels implement INotifyPropertyChanged interface, so that the property changes in ViewModel can be passed onto View. Let's take a sample property, to understand in better way:

public string SelectedName
    get { return _selectedName; }
        if (_selectedName != value)
            _selectedName = value;

Point to note here is the hard coded name of the property "SelectedName" within the RaisePropertyChanged method. Now due to requirement change, one of the developer came and change the name of a property from SelectedName to FirstName, and at the same time, he forgot to change the parameter of  RaisePropertyChanged method.  Now what will happen ???

This silly oversight will neither cause any compilation or run time errors, but our feature will simply not work. And such things are very difficult to detect until and unless there is a major break in functionality. So, now, how to get rid of such sort of issues???  

Tip comes here  

Instead of hard coding the property name, can't we go ahead and fetch the property name dynamically using Reflection APIs. Well, of course we can. 


To implement it in better way, let's go ahead and create an extension method to read this property name as:

 public static string GetPropertyName(this System.Reflection.MethodBase methodBase)
    return methodBase.Name.Substring(4);

Here Substring method is required to get rid of 'get_' and 'set_' at the start depending upon where it is called. 

So, by using this extension method, one will be able to raise property changed events without concern that in future your property name might change. 

After doing above changes, our property will look like: 

public string SelectedName 
    get { return _selectedName; }
        if (_selectedName != value)
            _selectedName = value;

I hope this small tip will be useful for you. 

Relevant Tracks 


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


About the Author

You may also be interested in...

Comments and Discussions

Generalsimpler Pin
stixoffire26-Aug-15 4:08
memberstixoffire26-Aug-15 4:08 
QuestionThis is no longer relevant with C# 6 Pin
Jacob Hands13-May-15 10:52
memberJacob Hands13-May-15 10:52 
Suggestion2 Suggestions Pin
Member 184235028-Nov-14 16:45
memberMember 184235028-Nov-14 16:45 
GeneralRe: 2 Suggestions Pin
Shweta Lodha28-Nov-14 19:52
memberShweta Lodha28-Nov-14 19:52 
QuestionUsing Reflection Pin
Member 973830618-Nov-13 22:28
memberMember 973830618-Nov-13 22:28 
AnswerRe: Using Reflection Pin
Shweta Lodha20-Nov-13 9:46
memberShweta Lodha20-Nov-13 9:46 
QuestionEasier way 2 Pin
Oliver A17-Nov-13 20:41
memberOliver A17-Nov-13 20:41 
AnswerRe: Easier way 2 Pin
Shweta Lodha20-Nov-13 9:43
memberShweta Lodha20-Nov-13 9:43 
QuestionThree questions Pin
leiyangge14-Nov-13 14:57
memberleiyangge14-Nov-13 14:57 
AnswerRe: Three questions Pin
Shweta Lodha16-Nov-13 23:53
memberShweta Lodha16-Nov-13 23:53 
GeneralEasier way Pin
Benny Tordrup14-Nov-13 2:12
memberBenny Tordrup14-Nov-13 2:12 
GeneralRe: Easier way Pin
Pete O'Hanlon14-Nov-13 7:12
protectorPete O'Hanlon14-Nov-13 7:12 
GeneralEasier wayRe: Easier way Pin
Benny Tordrup14-Nov-13 8:56
memberBenny Tordrup14-Nov-13 8:56 

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 | Terms of Use | Mobile
Web01 | 2.8.151002.1 | Last Updated 14 Nov 2013
Article Copyright 2013 by Shweta Lodha
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid