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

后端 未结 2 1088
情歌与酒
情歌与酒 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:20

    Try this:

    [DefaultValue( typeof( Padding ), "2, 2, 2, 2" )]
    public Padding LabelPadding
    {
        get { return _labelPadding; }
        set { _labelPadding = value; }
    }
    
    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题