Say I have the following class:
public MainFormViewModel
{
public String StatusText {get; set;}
}
What is the easiest smallest way to get my changes to StatusText to reflect to any controls that bind to it?
Obviously I need to use INotifyPropertyChanged, but is there a cool way to do it that does not clutter up my code? need lots of files? etc?
Note: If this is a dupe then I am sorry. I searched and could not find any thing but using T4 code Generation which does not sound easy (to setup at least).
Unfortunately C# doesn’t offer an easy mechanism to do that automatically… It has been suggested to create a new syntax like this :
But I doubt it will ever be included in the language…
A possible solution would to use an AOP framework like Postsharp, that way you just need to decorate your properties with an attribute:
(haven’t tried, but I’m pretty sure Postsharp allows you to do that kind of thing…)
UPDATE: OK, I managed to make it work. Note that it’s a very crude implementation, using reflection on a private field to retrieve the delegate… It could certainly be improved, but I’ll leave it to you 😉
Note that your class still need to implement the
INotifyPropertyChangedinterface. You just don’t have to explicitly raise the event in your property setters.