Coerce and validate
Posted: 14 Nov 2024, 10:58
I'm trying to understand the concept behind coerce and validate.
In DependencyProperty's constructor you can give it ValidateValueCallback.
When registering the DependencyProperty in UIElementData::RegisterProperty<>, I can also add ValidateValueCallback.
Also, in registering, the PropertyMetadata may be created with CoerceValueCallback and PropertyChangedCallback.
My questions:
1. Does ValidateValueCallback in registering overwrite the ValidateValueCallback in DependencyProperty's constructor.
2. What is the reason behind given those three callbacks in different locations.
3. What is the function of each callback:
a. I would expect all of them to be called when ever the DependencyProperty's data is changed (whether from with in the widget like in textbox or when programmatically changed or in any other way through the GUI if it's possible). Am I right?
b. I would expect ValidateValueCallback to be able to prevent the change by returning false. In which case PropertyChangedCallback won't be called at all.
c. I would expect CoerceValueCallback to change the actual value expressed by the DependencyProperty. However, what is the meaning of the return value. Does result of false will be the same as invalidating the value. Or perhaps it has another meaning>
d. In which order those are called?
Thanks
In DependencyProperty's constructor you can give it ValidateValueCallback.
When registering the DependencyProperty in UIElementData::RegisterProperty<>, I can also add ValidateValueCallback.
Also, in registering, the PropertyMetadata may be created with CoerceValueCallback and PropertyChangedCallback.
My questions:
1. Does ValidateValueCallback in registering overwrite the ValidateValueCallback in DependencyProperty's constructor.
2. What is the reason behind given those three callbacks in different locations.
3. What is the function of each callback:
a. I would expect all of them to be called when ever the DependencyProperty's data is changed (whether from with in the widget like in textbox or when programmatically changed or in any other way through the GUI if it's possible). Am I right?
b. I would expect ValidateValueCallback to be able to prevent the change by returning false. In which case PropertyChangedCallback won't be called at all.
c. I would expect CoerceValueCallback to change the actual value expressed by the DependencyProperty. However, what is the meaning of the return value. Does result of false will be the same as invalidating the value. Or perhaps it has another meaning>
d. In which order those are called?
Thanks