DevExpress.XtraLayout.LayoutControl 动态添加控件

// Create an item within a specified group,
// bound to a specified data field with the specified editor
private LayoutControlItem CreateItemWithBoundEditor(BaseEdit editor, object dataSource,
   string dataMember, LayoutControlGroup parentGroup) {
   editor.DataBindings.Add("EditValue", dataSource, dataMember);
   LayoutControlItem item = parentGroup.AddItem(dataMember, editor) as LayoutControlItem;
   return item;
}
private void CreateLayout() {
   //Add First Name and Last Name items
   LayoutControlItem itemFirstName = CreateItemWithBoundEditor(new TextEdit(), employeesSource,
      "FirstName", layoutControl1.Root);
   LayoutControlItem itemLastName = CreateItemWithBoundEditor(new TextEdit(), employeesSource,
      "LastName", layoutControl1.Root);
   // Move the Last Name to the right of the First Name
   itemLastName.Move(itemFirstName, InsertTypes.Right);

   // Add the Birthday group with a birthday editor inside
   LayoutControlGroup birthdayGroup = layoutControl1.AddGroup("Birthday Information");
   CreateItemWithBoundEditor(new DateEdit(), employeesSource, "BirthDate", birthdayGroup);

   // Add a tab with three address fields
   TabbedControlGroup tabbedGroup = layoutControl1.AddTabbedGroup();
   LayoutControlGroup addressGroup = tabbedGroup.AddTabPage("Address Details");
   string[] dataFields = new string[] { "Country", "City", "Address" };
   foreach (string dataField in dataFields)
      CreateItemWithBoundEditor(new TextEdit(), employeesSource, dataField, addressGroup);

   // Add a tab with a photo
   LayoutControlGroup groupPhoto = tabbedGroup.AddTabPage("Photo");
   CreateItemWithBoundEditor(new PictureEdit(), employeesSource, "Photo", groupPhoto);
}

 

再次添加的时候如果需要先清除之前的item,代码如下

            layoutControl.BeginUpdate();
            layoutControl.Controls.Clear();
            layoutControl.Root.Items.Clear();

            再次添加item的代码

           layoutControl.EndUpdate();

注意问题:

1、如果使用LayoutControlGroup直接添加Item

上面几行可以改为

    layoutControlGroup.BeginUpdate();

    layoutControlGroup.Clear();//无法释放Item中的Control,会把Control加在当前Form里,所以需要手动Dispose


   layoutControlGroup.EndUpdate();

完整代码例子:

List<BaseEdit> items = new List<BaseEdit>();

            
            layoutControlGroup2.BeginUpdate();
            layoutControlGroup2.Clear();

            for (int i = items.Count - 1; i >= 0;i-- )
            {
                if (items[i] != null)
                {
                    items[i].Dispose();
                    items.RemoveAt(i);
                }
            }
            LookUpEdit looupEdit = new LookUpEdit();
            items.Add(looupEdit);
            layoutControlGroup2.AddItem("fsds", looupEdit);
            layoutControlGroup2.EndUpdate();
2、注意layoutControlGroup.EndUpdate()相当于刷新了页面,如果之后再执行了其他重绘界面动作会导致显示问题,所以应该最后做本动作

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值