无法保存 ControlCollection 项目 [英] ControlCollection items could not be saved

查看:25
本文介绍了无法保存 ControlCollection 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 controlcollection 创建了一个控件.当我在设计时从属性窗口添加项目时.它完美地添加了.当我打开它时也是如此.添加的项目显示我.但是,当我关闭表单然后再次打开它时,项目被删除了.

I have created a control with controlcollection. When I add items from the property window at design time. It added perfectly. Also when I open it back. Added items shows me. But, When I close the form then open it again the items was removed.

现在我在集合中添加了两个项目.这些物品看起来很完美.

Now I have added two Items in the collection. The items was looking perfectly.

但是,当我打开 Form.Desigern.cs 文件时,缺少以下行.

But, When I open the Form.Desigern.cs file the following line is missing.

this.xWizardControl.Window.Controls.Add(this.xWizardPage1);
this.xWizardControl.Window.Controls.Add(this.xWizardPage2);

代码是这样的.

public class XWizardPageWindow : DevExpress.XtraEditors.XtraUserControl, ISupportInitialize
{
    private XWizardPageCollection _pages;
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public XWizardPageCollection Pages
    {
        get { return _pages; }
        set { _pages = value; }
    }
    public XWizardPageWindow()
    {
    }
    #region Override Methods
    protected override ControlCollection CreateControlsInstance()
    {
        if (_pages == null)
            _pages = new XWizardPageCollection(this);
        return _pages;
    }
    #endregion


    #region ISupportInitialize Members

    public void BeginInit()
    {
        //DO NOTHING
    }

    public void EndInit()
    {
        //DO NOTHING
    }

    #endregion
}

ControlCollection 类

public class XWizardPageCollection : System.Windows.Forms.Control.ControlCollection
{
    public delegate void XWizardPageEventHandler(object sender, XWizardPageEventArgs e);
    List<XWizardPage> _pages = new List<XWizardPage>();
    #region Constructor
    public XWizardPageCollection(System.Windows.Forms.Control owner): base(owner)
    {}
    #endregion

    #region Override Methods
    public override void Add(System.Windows.Forms.Control value)
    {
        base.Add(value);
        value.Dock = System.Windows.Forms.DockStyle.Fill;
        ((XWizardPage)value).BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
    }
    #endregion

    #region Destructor
    ~XWizardPageCollection()
    {
        GC.SuppressFinalize(this);
    }
    #endregion
}

推荐答案

首先,永远不要改变 ControlCollection 一旦由 CreateControlsInstance.所以Pages 属性应该定义为ReadOnly.

First, one should never change the ControlCollection once created and returned by CreateControlsInstance. So the Pages property should be defined as ReadOnly.

其次,当使用 可见您正在告诉代码生成器创建一个我们不想要的 Pages 的新实例.所以更改 DesignerSerializationVisibilityAttributeVisibleContent,代码生成器将为对象(页面)的内容生成代码,而不是为对象本身生成代码.

Secondly, when using Visible you're telling the code generator to create a new instance of Pages, which we don't want. So change the DesignerSerializationVisibilityAttribute from Visible to Content and the code generator will produce code for the contents of the object (Pages), rather than for the object itself.

这篇关于无法保存 ControlCollection 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆