Repository navigation
Description
Background and motivation
Some WinForms control properties are decorated with a DefaultValueAttribute. When control is created in the designer, and the property had not been initialized yet, property browser reads attribute value and shows it in the property browser. If the user changes such a value in the property browser, design time serialization writes the user-specified value into the InitializeComponent method in the code-behind file. If the user does not assign specific value to such a property in the property browser, this property will not be serialized into the code-behind file, as controlled on line 1176 by the type description provider. Default value is also used to reset property value in the property browser.
At the runtime, properties that are not assigned to in the InitializeComponent method, are initialized to default runtime values.
Design time components assume that the runtime default value is the same as the design time one i.e., in the case when the attribute is used, the same as the attribute value. If this assumption is not upheld, the reset option in the property browser in the IDE will not work, and InitializeComponent will explicitly serialize default values into the InitializeComponent method instead of letting the control pick up the runtime default on the initialization.
The motivation
For Height or Width properties, the default runtime value is dependent on the application font. Some of these values had been calculated and hardcoded before we have changed the default font. Currently some of the design time default values defined in this attribute are out of sync with the runtime default values that are explicitly assigned in the constructor.
Example
Design time default value -
https://github.com/dotnet/winforms/blob/62edc1ab7b601bee58199fdea2580a54cf5cd25b/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewRow.cs#L212-L218
The runtime value is dynamically calculated based on the default font while the designer assumes a constant value.
We received several bug report that follow this pattern:
- when designer is shown, control is instantiated, and the runtime default value is assigned to the property.
- when control is serialized, TypeDescriptor compares the actual property value (the runtime default) against the value in the attribute, sees that they are different and adds a line that sets this value to the
InitalizeComponent.
Problem: User had not edited this value and expects that height is picked up from the font, user changes the control font on the startup and the height is not scaled for the new font.
API Proposal
Use an alternative mechanism to define the default design time values. This is already used by other properties and is supported in the TypeDescriptor, see line 1170.
- remove the default value attribute from properties that have dynamic default values.
- add
Reset<Property>andShouldSerialize<Property>methods that compare actual value against the dynamic runtime default.
This is a pseudocode representation of the pattern.
before:
namespace System.Windows.Forms;
public class SomeControl : Control
{
private const int DefaultHeight = 22;
public SomeControl()
{
Height = DefaultHeight;
}
[DefaultValue(DefaultHeight)]
public int Height {set; get;}
}after:
namespace System.Windows.Forms;
public class SomeControl : Control
{
public SomeControl()
{
Height = DefaultHeight;
}
private int DefaultFont
{
get
{
// Calculate height to accommodate text that uses the default font, i.e. use Control.DefaultFont.Height
}
}
public int Height {set; get;}
private void ResetHeight()
{
Height = DefaultHeight;
}
private bool ShouldSerializeHeight()
{
return Height != DefaultHeight;
}
}API Usage
This is a proposal to remove an attribute and replace it with a different mechanism that supports the same design time functionality.
Alternative Designs
No response
Risks
This attribute implements design time functionality that should be accessed through the TypeDescriptor. WinForms team considers this a grey area case of breaking changes because design time functionality is preserved.
Activity
[-][API Proposal]: Removed the DefaultValueAttribute from properties that need dynamically calculated default values in WinForms controls[/-][+][API Proposal]: Remove the DefaultValueAttribute from properties that need dynamically calculated default values in WinForms controls[/+]Tanya-Solyanik commentedon Feb 24, 2023
Duplicate of dotnet/winforms#8599