INotifyPropertyChanged oddity

  • Thread starter Thread starter Paul H
  • Start date Start date
P

Paul H

Hi,

I have a simple class that implements INotifyPropertyChanged.

I can create an instance of the class and bind it to a textbox.
Changing a property of this object automatically updates the textbox as
expected. Works great.

Next, I created a custom control based on TextBox that implements a
'MyText' get/set. This just sets/gets the Text property.

Again, this works well. But I noticed something odd while debugging.

Changing a single property causes *all* controls bound to this object to
re-bind (i.e. the get method is called). Only 1 PropertyChanged event
is fired, for the property that has changed.

So if I have 20 properties and I update them all (e.g. loading from
disk) I see 20*20 calls to the get method of MyText.

Is this expected behaviour?
 
Hi Paul,

It is expected. You can limit unwanted PropertyChanged events by checking
if the property has actually changed and only raise the event on actual
change.

set
{
if(_property == value)
return;

_property = value;
// raise PropertyChanged
}
 
Hi,

I have a simple class that implements INotifyPropertyChanged.

I can create an instance of the class and bind it to a textbox.
Changing a property of this object automatically updates the textbox as
expected.  Works great.

Next, I created a custom control based on TextBox that implements a
'MyText' get/set.  This just sets/gets the Text property.

Again, this works well.  But I noticed something odd while debugging.

Changing a single property causes *all* controls bound to this object to
  re-bind (i.e. the get method is called).  Only 1 PropertyChanged event
is fired, for the property that has changed.

So if I have 20 properties and I update them all (e.g. loading from
disk) I see 20*20 calls to the get method of MyText.

Is this expected behaviour?

Hi,

Yes, that is the way it works, if you look into the
INotifiedPropertyChanged interface it provides a single event,
PropertyChanged which has a property saying which property changed. so
basically any control that is binded to a property hook into this
event which is fired when one property (any property) is changed.
 
Back
Top