继承控件不会在表单设计器生成的代码中生成代码

时间:2013-03-30 18:22:08

标签: c# winforms

我正在尝试继承DevExpress PictureEdit(但是,我认为这个问题不是特定于DevExpress)。我添加了一些属性。

public class RepositoryItemFotoEdit:RepositoryItemPictureEdit
{
    private Size _imageSize;
    public Size ImageSize
    { 
        get
        {
            return _imageSize;
        } 
        set 
        { 
            _imageSize = value;
            OnPropertiesChanged();
        }
    }

    [DefaultValue(InterpolationMode.HighQualityBicubic)]
    public new InterpolationMode PictureInterpolationMode
    {
        get { return base.PictureInterpolationMode; }
        set { base.PictureInterpolationMode = value; }
    }

    [DefaultValue(PictureSizeMode.Zoom)]
    public new PictureSizeMode SizeMode
    {
        get { return base.SizeMode; }
        set { base.SizeMode = value; }
    }        

    public RepositoryItemFotoEdit()
    {
        PictureInterpolationMode = InterpolationMode.HighQualityBicubic;
        SizeMode = PictureSizeMode.Zoom;            
    }
}



public sealed class FotoEdit : PictureEdit
{
    private FotoMenu _menu;

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (_menu != null)
                _menu.Dispose();
        }
        base.Dispose(disposing);
    }

    public override Image Image
    {
        get
        {
             return base.Image;
        }
        set
        {
            if(Properties.ImageSize.Width == 0 || Properties.ImageSize.Height == 0 || value == null)
                base.Image = value;
            else
            {
                var heightProp = (double) Properties.ImageSize.Height/value.Height;
                var widthProp = (double) Properties.ImageSize.Width/value.Width;
                var prop = heightProp < widthProp ? heightProp : widthProp;

                base.Image = ImageHelper.ResizeImage(value, (int)(prop*value.Width), (int)(prop*value.Height));
            }
        }
    }

    public new RepositoryItemFotoEdit Properties
    {
        get { return base.Properties as RepositoryItemFotoEdit; }
    }

    protected override PictureMenu Menu
    {
        get { return _menu ?? (_menu = new FotoMenu(this)); }
    }

    protected override EditorClassInfo EditorClassInfo
    {
        get
        {
            var beci = base.EditorClassInfo;
            var eci = new EditorClassInfo("FotoEdit", typeof (FotoEdit), typeof (RepositoryItemFotoEdit),
                                          beci.ViewInfoType, beci.Painter, beci.DesignTimeVisible);
            return eci;
        }
    }
}

属性ImageSize出现在PropertyEditor上。

enter image description here

但是,如果我将其更改为某个值(例如150; 150),则不会在表单设计器生成的代码中生成代码。

请注意,ImageSize位于Properties属性中。

// 
// fotoEdit1
// 
this.fotoEdit1.Cursor = System.Windows.Forms.Cursors.Default;
this.fotoEdit1.Location = new System.Drawing.Point(583, 370);
this.fotoEdit1.Name = "fotoEdit1";
this.fotoEdit1.Size = new System.Drawing.Size(150, 150);
this.fotoEdit1.TabIndex = 18;

这将导致ImageSize值恢复为0; 0。如何解决这个问题?

编辑:

我试图将[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]添加到Properties属性,因为有人(我忘记了他的名字并删除了他的答案)。导致Properties属性内的属性出现在设计器生成的代码中,但覆盖的除外。

enter image description here

我的设计师生成的代码如下所示:

        // 
        // fotoEdit1
        // 
        this.fotoEdit1.Cursor = System.Windows.Forms.Cursors.Default;
        this.fotoEdit1.Location = new System.Drawing.Point(583, 370);
        this.fotoEdit1.Name = "fotoEdit1";
        this.fotoEdit1.Properties.Padding = new System.Windows.Forms.Padding(5, 2, 0, 0);
        this.fotoEdit1.Size = new System.Drawing.Size(150, 150);
        this.fotoEdit1.TabIndex = 18;

请注意,Properties.Padding会显示,但Properties.ImageSize不会显示。

1 个答案:

答案 0 :(得分:0)

我只是通过将[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]添加到Properties属性并将[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]添加到Properties.ImageSize属性来解决问题。