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

后端 未结 1 688
时光说笑
时光说笑 2021-02-15 03:32

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

1条回答
  •  星月不相逢
    2021-02-15 04:06

    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

    0 讨论(0)
提交回复
热议问题