ControlCollection items could not be saved

我们两清 提交于 2019-12-24 17:17:21

问题


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.

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);

The code is looks like this.

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 Class

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
}

回答1:


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

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.



来源:https://stackoverflow.com/questions/22268647/controlcollection-items-could-not-be-saved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!