Easier way to do a NotifyOfPropertyChange

Sep 27, 2010 at 1:13 PM

Hi guys!

Do you know a way to only use only one variable and still be able to call NotifyOfPropertyChange.

This is the way I usually run into the problem: I create a property in the ViewModel for the View to bind to, say: public string MyProperty { get; set; }. It works fine until I need to bind something else to the property. Then a change of the property (in the View) needs to throw a NotifyOfPropertyChange. So I do like this:

private string myProperty;
public string MyProperty { get { return myProperty; } set { myProperty = value; NotifyOfPropertyChange(“MyProperty”); }

I don’t really like this though. It seems superfluous to have both myProperty and MyProperty when I really just need one variable. Also: the process of converting the first expression to the second is a mechanical one which usually gives me a hint that the computer should do this automatically.

Anyone knows anything about this?



Sep 27, 2010 at 1:38 PM

I think most people often create a custom VS snippet for writing these properties. Other than that, the only way to make this easier is to use AOP through something like DynamicProxy. There's nothing out-of-the-box to make this easier. It's quite a tricky and annoying issue, I know.

Sep 27, 2010 at 3:53 PM

Thanx for the quick anwer.

I think I will probably create a snippet for now and look more into DynamicProxy in the future.

Great program CM btw :)