tags:

views:

89

answers:

3

Is there a way to use INotifyPropertyChanged with auto-properties? Maybe an attribute or something other, not obvious to me.

public string Demo{
    get;set;
}

For me, auto-properties would be an extremely practicall thing, but almost always, I have to raise the PropertyChanged-event if the property value has been changed and without a mechanism to do this, auto-properties are useless for me.

+2  A: 

There's no built-in mechanism to do this. Something like PostSharp would likely be able to add something like this for you (or Mark Gravell's HyperDescriptor, if you're just interested in making this databinding-aware).

Adam Robinson
+1  A: 

INotifyPropertyChanged and DependencyProperties have certainly made properties a lot less fun. The best solution I've found is good snippets. Such as the ones that come in the Silverlight contrib project

Matt Greer
+2  A: 

Someone has proposed a change to implement this:

class Person : INotifyPropertyChanged
{
    // "notify" is a context keyword, same as "get" and "set"
    public string Name { get; set; notify; }
}

Source

NOTE: I know this doesn't work. I'm quoting something that someone has proposed.


In the meantime it's something you would have to code yourself, such as this implementation on Code Project that uses a custom attribute and aspect orientated methods to give this syntax:

[NotifyPropertyChanged] 
public class AutoWiredSource
{ 
   public double MyProperty { get; set; } 
}
ChrisF
Hmmmm, my first thought is that's a great idea. But I wonder how well it'd really hold up if implemented.
Matt Greer
@Matt - I'm not sure either, but when I was looking for the attribute example I came across it and thought I'd share.
ChrisF
I'm not sure what I'd think of this...while convenient, I'd prefer to keep the library components out of the language. An obvious exception is the `using` block, but what happens if (when) we come up with a different method of notifying property changes?
Adam Robinson
@Adam - I suspect that's one reason why the proposed change is just that. I think I prefer the attribute mechanism myself.
ChrisF