How does one have the C# designer know the default property for a Padding or other object/struct in C#

后端 未结 2 1095
情歌与酒
情歌与酒 2021-02-15 03:42

How does one tell the designer the default value of a Property when it is not one of the types supported by DefaultValue()? For instance, a Padding, or

2条回答
  •  被撕碎了的回忆
    2021-02-15 04:24

    Try this:

    private static Padding DefaultLabelPadding = new Padding(2);
    private internalLabelPadding = DefaultLabelPadding;
    public Padding LabelPadding { get { return internalLabelPadding; } set { internalLabelPadding = value; LayoutNow(); } }
    
    // next comes the magic
    bool ShouldSerializeLabelPadding() { return LabelPadding != DefaultLabelPadding; }
    

    The property browser looks for a function named ShouldSerializeXYZ for each property XYZ. Whenever ShouldSerializeXYZ returns false, it doesn't write anything during code generation.

    EDIT: documentation:

    • [1]: http://msdn.microsoft.com/en-us/library/ms973818.aspx
    • [2]: http://msdn.microsoft.com/en-us/library/53b8022e.aspx

提交回复
热议问题