最近有几个同学询问动态表单上面动态添加控件的用法,把以前写的示例贴出来看看,仅供参考备查。
示例场景:在一个动态表单面板上面动态增加一些LinkButton按钮,其中数据来源dycGoodsType_Root来自数据库查询的结果,有几个,就增加几个LinkButton按钮。
【begin using 部分】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using BOSControlModel = Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using System.ComponentModel;
using Kingdee.K3.SCM.ServiceHelper;
using Kingdee.BOS;
using Kingdee.K3.SCM.CP.Core;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.K3.SCM.CP.BusinessEntity;
using Kingdee.BOS.Orm.Metadata.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.BOS.Orm;
using Kingdee.BOS.BusinessEntity;
using Kingdee.BOS.Core.Metadata.ControlElement;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.VerificationHelper;
【begin 插件类部分】
private LayoutInfo myLayoutInfoClone = null;
private BusinessInfo myBusinessInfoClone = null;
///
/// 布局设置
///
///
public override void OnSetLayoutInfo(SetLayoutInfoArgs e)
{
InitData();
#region 动态创建商品类别第一版 LinkButton
//商品类别-- (弹出Show类别菜单按钮)
myLayoutInfoClone.Add(ConstructLinkBtnAppearance(linkBtn_ShowTypeMenu, 商品类别", headEntity.Key));
//全部-- (一级类别过滤过滤使用按钮)
myLayoutInfoClone.Add(ConstructLinkBtnAppearance(linkBtn_AllKey, "全部", headEntity.Key));
foreach (DynamicObject dyItem in dycGoodsType_Root)
{
ConstructLinkBtnAppearance(Convert.ToString(dyItem["FId"]), Convert.ToString(dyItem["FName"]), headEntity.Key);
myLayoutInfoClone.Add(ConstructLinkBtnAppearance(Convert.ToString(dyItem["FId"]), Convert.ToString(dyItem["FName"]), headEntity.Key));
}
e.LayoutInfo = myLayoutInfoClone;
e.BillLayoutInfo = myLayoutInfoClone;
#endregion
base.OnSetLayoutInfo(e);
}
///
/// 绑定数据后事件
///
///
public override void AfterBindData(EventArgs e)
{
#region 动态创建商品类别第一版 LinkButton --第二版改为树承载商品类别
BOSControlModel.Container panel = this.View.GetControl
LayoutInfo layout = new LayoutInfo();
int top = 8;
int left = 15;
//商品类别-- (弹出Show类别菜单按钮)
LinkBtnAppearance linkTypeAP_ShowTypeMenu = (LinkBtnAppearance)myLayoutInfoClone.GetAppearance(linkBtn_ShowTypeMenu);
layout.Add(SetLinkBtnLayout(ref top, ref left, linkTypeAP_ShowTypeMenu));
//全部-- (一级类别过滤过滤使用按钮)
LinkBtnAppearance linkTypeAP_All = (LinkBtnAppearance)myLayoutInfoClone.GetAppearance(linkBtn_AllKey);
layout.Add(SetLinkBtnLayout(ref top, ref left, linkTypeAP_All));
int i = 0;
foreach (DynamicObject dyItem in dycGoodsType_Root)
{
LinkBtnAppearance linkTypeAP = (LinkBtnAppearance)myLayoutInfoClone.GetAppearance(Convert.ToString(dyItem["FId"]));
layout.Add(SetLinkBtnLayout(ref top, ref left, linkTypeAP));
i++;
}
if (layout != null)
{
panel.AddControls(layout);//加入容器
}
#endregion
}
///
/// 初始化数据、变量
///
private void InitData()
{
if (metaData == null)
{
myLayoutInfoClone = (LayoutInfo)this.View.OpenParameter.FormMetaData.GetLayoutInfo().CloneWithControlField();
myBusinessInfoClone = (BusinessInfo)ObjectUtils.CreateCopy(this.View.OpenParameter.FormMetaData.BusinessInfo);
if (headEntity == null)
{
headEntity = (Entity)ObjectUtils.CreateCopy(this.View.BusinessInfo.Entrys[0]);
}
}
}
///
/// 设置LinkButton的布局信息
///
///
///
///
///
private LinkBtnAppearance SetLinkBtnLayout(ref int top, ref int left, LinkBtnAppearance linkTypeAP)
{
BOSControlModel.Container goodsTypePanel = this.View.GetControl
int lcid = this.Context.UserLocale.LCID;
linkTypeAP.Top[lcid] = top.ToString();
linkTypeAP.Width[lcid] = lcid != 1033 ? (linkTypeAP.Caption[lcid].Length * 12).ToString() : (linkTypeAP.Caption[lcid].Length * 4).ToString();
linkTypeAP.Left[lcid] = left.ToString();
left = Convert.ToInt32(linkTypeAP.Left[lcid]) + Convert.ToInt32(linkTypeAP.Width[lcid]) + 20;
if (left > goodsTypePanel.ControlAppearance.Width)
{
top = top + 20;
left = 80;
}
return linkTypeAP;
}
///
/// 构造动态LinkButton控件(商品类别)布局
///
///
///
///
///
private LinkBtnAppearance ConstructLinkBtnAppearance(string key, string caption, string entityKey)
{
int lcid = this.Context.UserLocale.LCID;
LinkBtnAppearance linkFieldAP = new LinkBtnAppearance();
linkFieldAP.Key = key;
linkFieldAP.BackColor = "#FFBB00";
linkFieldAP.Caption[lcid] = caption; ;
linkFieldAP.EntityKey = entityKey;
return linkFieldAP;
}
///
/// 构造Panel的布局
///
///
///
///
///
private PanelAppearance ConstructPanelAppearance(string key, string entityKey, string ParentContainerKey)
{
int lcid = this.Context.UserLocale.LCID;
PanelAppearance panelAp = new PanelAppearance();
panelAp.Key = key;
panelAp.Container = ParentContainerKey;
panelAp.EntityKey = entityKey;
return panelAp;
}
插件示例参考完整.zip(7.41KB)