0

I have a property defined with no explicit mention of backing field in a way like this :

//How to fire RaisePropertyChanged("Name") from setter
public string Name { get; set; } 

How exactly do I fire a RaisePropertyChanged event in the setter of this property ? I know how to do it when there is a private backing field and with a public property encapsulating it.

Is a multi line setter allowed in this scenario ?

AymenDaoudi
  • 7,811
  • 9
  • 52
  • 84
Nick
  • 586
  • 2
  • 7
  • 22

2 Answers2

4

No, you can't do this with an automatic property. You'll need to have a backing field and define both the getter and setter yourself, and raise the event in the setter as you described.

TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • +1 because it is true without a lot of tricks. You can do it with reflection though... – MichaC Jan 03 '14 at 16:39
  • @MichaC Not just reflection but code emission, no? I'm assuming the OP doesn't want to go there ;) BUT if so I do have experience with this..! – TypeIA Jan 03 '14 at 16:42
0

That's an Automatic Property, you need to have a backing field.

private string _name;
public string Name
{
    get { return name; }
    set 
    { 
        name = value; 
        RaisePropertyChanged("Name"); 
    }
}
Nick
  • 586
  • 2
  • 7
  • 22
AymenDaoudi
  • 7,811
  • 9
  • 52
  • 84