views:

59

answers:

2

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
        };
    }
}
+1  A: 

If you have a property of a non-simple type, the Properties window will automatically show a Plus button to expand the instance.

To provide a custom dropdown list, you need to write a UITypeEditor.

SLaks
The above code shows `Style: Sai.Phone.Client.GradientPanel+PanelStyle` but it's disabled, and has no Plus button.
Daniel Rasmussen
+1  A: 

Structs are difficult to deal with in a PropertyGrid because they are value types. Assigning a field or a property of a struct has no effect, the entire struct value needs to be assigned.

Note how Font is a class, setting individual Font properties in the grid is not a problem. Font has a TypeConverter (System.Drawing.FontConverter) which takes a string and converts it to a Font. That's why you can edit the Font value directly instead of having to expand the node and edit individual properties. It also has a UITypeEditor, System.Drawing.Design.FontEditor, which presents the dialog when you click the button with the dots.

Which is what you probably need to do for your property if you want to use to pick from predefined styles. A good example of a UITypeEditor for a struct is System.Drawing.Design.ColorEditor, use Reflector to have a look at the class. To just expose the properties you need a TypeConverter, a good example of one for a struct type is System.Drawing.PointConverter.

Hans Passant