I
Iulian Ionescu
Here is how I typically define this:
protected virtual OnSomePropertyChanged(EventArgs e)
{
if (this.SomePropertyChanged!=null)
{
this.SomePropertyChanged(this, new EventArgs());
}
}
public int SomeProperty
{
get {return this.someField;}
set {
if (this.someField!=value)
{
this.someField = value;
this.OnSomePropertyChanged(new EventArgs());
}
}
public event EventHandler SomePropertyChanged;
This being said, I would like to ask a few questions
about good practices regarding this:
1) Is it always necesary to have an event for each public
property even if it is very less likely to be listened by
anyone? For example: BorderStyleChanged. There is almost
no reason for anybody to listen to this and do something
about it...
2) If the change of the property requires additional
actions (like the invalidation of some areas or childred,
some layout recalculations, and so on), where should
these action be put: in the OnSomeProperty change method,
before or after the raise of the event? Or in the
property's set method?
I see some pros and cons: putting it in the On... allows
the inheritor to completely override all actions that
happen due to the property being changed. However, to
assure that the control (if it's a control) is properly
redrawn perhaps the call to Invalidate should be in the
set method?
This is it, if anybody has some good practices regarding
this I would be happy to hear...
Thank you,
Iulian
protected virtual OnSomePropertyChanged(EventArgs e)
{
if (this.SomePropertyChanged!=null)
{
this.SomePropertyChanged(this, new EventArgs());
}
}
public int SomeProperty
{
get {return this.someField;}
set {
if (this.someField!=value)
{
this.someField = value;
this.OnSomePropertyChanged(new EventArgs());
}
}
public event EventHandler SomePropertyChanged;
This being said, I would like to ask a few questions
about good practices regarding this:
1) Is it always necesary to have an event for each public
property even if it is very less likely to be listened by
anyone? For example: BorderStyleChanged. There is almost
no reason for anybody to listen to this and do something
about it...
2) If the change of the property requires additional
actions (like the invalidation of some areas or childred,
some layout recalculations, and so on), where should
these action be put: in the OnSomeProperty change method,
before or after the raise of the event? Or in the
property's set method?
I see some pros and cons: putting it in the On... allows
the inheritor to completely override all actions that
happen due to the property being changed. However, to
assure that the control (if it's a control) is properly
redrawn perhaps the call to Invalidate should be in the
set method?
This is it, if anybody has some good practices regarding
this I would be happy to hear...
Thank you,
Iulian