How can I write my own structs which can be used as a property during design time? I need to be able to specify a default value, and have a selectable list of pre-defined structs for the designer to pick from, in much the same way as Color properties are implemented.
Furthermore, how can I do this with classes, like Font does? How can you specify sub-properties in the property window?
I'm writing a custom control which has a lot of different visual-type elements, such as gradient colors, widths, percentages, etc. I want these to all be customizable, but also to be able to be set all at once with different Styles
. I can do this at run-time by making a Style
struct property and having it change all the other properties in the setter. What I would like is for users at design time to be able to select pre-defined Styles
such as "Light Blue," "Dark Grey," etc., each of which would set all the other UI properties (the gradients, etc.) If I could have all the UI properties appear under the "Style" property, much like "Bold" and "Italic" appear under "Font," that would be ideal.
Code:
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public partial class GradientPanel : UserControl
{
public PanelStyle Style
{
get
{
return new PanelStyle()
{
BackgroundFade = this.BackgroundFade,
EdgeColor = this.EdgeColor,
BorderColor = this.BorderColor,
EdgeWidth = this.EdgeWidth,
LowerColor = this.LowerColor,
UpperColor = this.UpperColor
};
}
set
{
this.SuspendLayout();
this.BackgroundFade = value.BackgroundFade;
this.EdgeColor = value.EdgeColor;
this.BorderColor = value.BorderColor;
this.EdgeWidth = value.EdgeWidth;
this.LowerColor = value.LowerColor;
this.UpperColor = value.UpperColor;
this.ResumeLayout();
}
}
....
public struct PanelStyle
{
public float BackgroundFade;
public Color EdgeColor;
public int EdgeWidth;
public Color BorderColor;
public Color UpperColor;
public Color LowerColor;
public static PanelStyle System = new PanelStyle()
{
BackgroundFade = .7f,
EdgeColor = SystemColors.Window,
BorderColor = SystemColors.WindowFrame,
EdgeWidth = 6,
LowerColor = SystemColors.Control,
UpperColor = SystemColors.Window
};
}
}