I know that component-model indicates whether a property has a default value or not, by means of ShouldSerializeValue method of Pr开发者_如何学编程opertyDescriptor.
The base windows-forms Control
class, has some properties like ForeColor, BackColor and Font, that defaults to the same value of the parent, but I could not find any TypeDescriptor
or PropertyDescriptor
that provides these default values. The Control class does not implement ICustomTypeDescriptor nor has a TypeDescriptionProviderAttribute.
How does the Control class indicates that these properties should be serialized or not? Where does it provide the PropertyDescriptors for these properties??
Thanks!!!
They don't have default values. These properties are "ambient" properties. The Control class detects that a property assignment has occurred for them. If that never happened, it uses the corresponding property from the Parent. Which is nice, it ensures child controls use the same colors and font as their container.
There is a ShouldSerializeForeColor() method in the Control class. It is internal and can't be overridden by user code. Same for the other properties. Have a look-see with Reflector or the .NET Reference Source.
The MSDN Library documents them like this:
Windows Forms controls use ambient properties so child controls can appear like their surrounding environment. An ambient property is a control property that, if not set, is retrieved from the parent control. If the control does not have a Parent, and the property is not set, the control attempts to determine the value of the ambient property through the Site property. If the control is not sited, if the site does not support ambient properties, or if the property is not set on the AmbientProperties, the control uses its own default values. Typically, an ambient property represents a characteristic of a control, such as BackColor, that is communicated to a child control. For example, a Button will have the same BackColor as its parent Form by default. Ambient properties provided by the Control class include: Cursor, Font, BackColor, ForeColor, and RightToLeft.
精彩评论