我用 controlcollection 创建了一个控件。当我在设计时从属性窗口添加项目时。它完美地添加了。当我打开它回来时。添加的项目显示我。但是,当我关闭表单然后再次打开它时,项目被删除了。
现在我在集合中添加了两个项目。 这些物品看起来很完美。
但是,当我打开Form.Desigern.cs
文件时,缺少以下行。
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
}