2011-07-08 29 views
1

注意:此问题中描述的所有控件都是DEVEXPRESS控件。 我试图在DevExpress LayoutControl上以编程方式将LayoutControlItemsEmptySpaceItems添加到LayoutControlGroup如何使用DevExpress将具有相同宽度的EmptySpaceItems和LayoutControlItems添加到LayoutControlGroup?

我的项目需求要求我有一个LayoutControlGroup面板,该面板依赖于从布局控件上的另一个控件中选择的一组过滤器项目。如果没有选择过滤器,则不会显示任何`LayoutControlItems。如果选择了一个或多个过滤器,并根据所选内容将一个或多个控件添加到组中。

,我试图做到这一点是以下方式:

1)为LayoutControl我已经创建了LayoutControlGroupLayoutControlItem S上的设计师。有6个总,并且每个LayoutControlItem包含PanelControl包含ListBoxControl

2)当表单initalized我使用LayoutControlItem.HideFromCustomization()方法隐藏每个LayoutControlItemsLayoutControlGroup

3)用户选择一个过滤器或一组过滤器后,我运行下面的代码,尝试从左到右将控制项恢复到组。

this.layoutGroupScenarioPortfolios.BeginUpdate(); 

LayoutControlItem layoutControlToAdd; 
LayoutControlItem lastLayoutControlItem = null; 

for (int loop = 0; loop < selectedFilters.Length; loop++) 
{ 
    layoutControlToAdd = LayoutControlItemFactoryUtil(selectedFilters[loop]); 

    if (layoutControlToAdd == null) 
    { 
      continue; 
    } 

    if (loop < 1) 
    { 
    layoutControlToAdd.RestoreFromCustomization(this.layoutControlGroupSelectedFilters); 
    } 
    else 
    { 
      layoutControlToAdd.RestoreFromCustomization(lastLayoutControlItem, DevExpress.XtraLayout.Utils.InsertType.Right); 
    } 

    lastLayoutControlItem = layoutControlToAdd; 
} 

for (int loop = 0; loop < numOfEmptySpaceItemsNeeded; loop++) 
{ 
     layoutControlToAdd = new EmptySpaceItem(this.layoutControlGroupSelectedFilters) 
     { 
      Owner = this.layoutControlGroupSelectedFilters.Owner 
     }; 

     layoutControlToAdd.RestoreFromCustomization(lastLayoutControlItem, DevExpress.XtraLayout.Utils.InsertType.Right); 
     lastLayoutControlItem = layoutControlToAdd; 
} 

this.layoutControlGroupSelectedFilters.TextVisible = true; 
this.layoutGroupScenarioPortfolios.EndUpdate(); 

正如您从代码中看到的一个循环将相应的ListControlBox添加到组中。第二个循环尝试添加空白项目以确保列表框控件不占用整个组。在此代码的末尾,应该有6个项目跨越组控制,每个控件中的宽度都相同。

问题是,添加的第一个控件占用了组框的空间的一半,而其他5个项目同样适用于组框的剩余一半。

在第一个循环中,RestoreFromCustomization()方法的一个参数是否使用了正确的方法?

回答

1

我建议把控件放在运行时。 LayoutControl将管理LayoutControlGroups和EmptySpaceItems。

这里是我写的地方用户控件到LayoutControl在运行时代码:

LayoutControlItem lastItem = null; 
int RowWidth = 0; 

    public void AddMyControl() 
      { 
       MyControl myControl = new MyControl(""); 
       myControl.Name = Guid.NewGuid().ToString(); 

       LayoutControlItem item = new LayoutControlItem(); 
       item.Name = Guid.NewGuid().ToString(); 
       item.Text = ""; 

       MyLayoutControl.BeginUpdate(); 
       //We need to determine where to insert the new item. Right or Below. If there is 
       //space on the right we insert at Right else we just add the item. 
       if(lastItem == null || lastItem != null && (MyLayoutControl.Width - UserControlWidth) < RowWidth) 
       { 
        MyLayoutControl.AddItem(item); 
        RowWidth = item.MinSize.Width; 
       } 
       else 
       { 
        MyLayoutControl.AddItem(item, lastItem, DevExpress.XtraLayout.Utils.InsertType.Right); 
       } 
       item.Control = myControl; 
       RowWidth += item.MinSize.Width; 
       lastItem = item; 
       item.Name = " "; 
       MyLayoutControl.BestFit(); 
       MyLayoutControl.EndUpdate(); 
      } 

如果你只需要从左到右控件,FlowLayoutPanel的会更适用。有时LayoutControl很难处理。我最终使用了flowlayoutpanel,因为它更容易处理。

+0

Aseem,谢谢你的回应!我怀疑有人会帮助我。我会把你有的和你一起工作。 – AndHeCodedIt

+1

如果事情仍然无法解决,请回复! :) –

相关问题