The system automatically fires the WriteProperties
events whenever it thinks you may need their services. To ensure that
the system knows a property has changed, you have to call the PropertyChanged
method. An example of this would be when you change the value of a
variable that implements the value of a property. The system will have no
way of knowing that this variable is connected with a property, and therefore it will not fire
event based solely on the change you have made.
In such cases, you can call the PropertyChanged method. This method informs
the system that a particular property has changed and so ensures that the
WriteProperties event will fire before the current instance of the control is destroyed.
If you've written the appropriate code in the WriteProperties xvent,
then your property values will be stored in the Property Bag.
You should call the UserControl's
PropertyChanged method whenever you do something in code that will cause a change to
a property whose value you wish to persist. The most typical place for you to call the
PropertyChanged method would be in a Property Let or Property
Set procedure (see Listing 13.8). Note that we check the CanPropertyChange
method that we discuss in "Calling the CanPropertyChange Method Before Allowing
a Property Value to Change."
CALLING THE PropertyChanged METHOD TO ENSURE THAT WRITEPROPERTIES WILL FIRE
Property Let Celsius(sValue As Single)
If CanPropertyChange("Celsius") Then
'assign incoming value to be stored
'in Private variable
m_Celsius = sValue
'invoke UserControl's PropertyChanged method
'so it knows to trigger WriteProperties and
'store new value
'perform other housekeeping specific to this
Slider1.Value = m_Celsius