DevExpress学习笔记(六)-Edit树节点
DevExpress学习笔记
Edit树节点的列
所谓Edit树节点的列,有如下两层含义:(1)根据某标识决定节点的某列Cell的形式,例如Cell可选择为时间控件,DropDownList控件等等;(2)与第一点类似,但是需要在运行时才动态决定Cell的类型,例如鼠标单击某Cell的时候,决定此Cell为哪种控件。因此将这两种类型概括为“静态Edit”和“动态Edit”。
一 静态Edit
静态Edit在DevExpress的官方示例中有一个很棒的示例,Demo运行的效果图如下:
分析此Demo的源码,首先看下初始化数据部分,函数为InitData:
View Code
//这个函数构造了一个包含11元素的Record数组,并将此数组作为TreeList的数据源。 private void InitData() { Record[] records = new Record[11]; records[0] = new Record("Product Name", "Chai", "Teatime Chocolate Biscuits", "Ipoh Coffee", 0); records[1] = new Record("Category", 1, 2, 1, 1); records[2] = new Record("Supplier", "Exotic Liquids", "Specialty Biscuits, Ltd.", "Leka Trading", 2); records[3] = new Record("Quantity Per Unit", "10 boxes x 20 bags", "10 boxes x 12 pieces", "16 - 500 g tins", 3, 0); records[4] = new Record("Unit Price", 18.00, 9.20, 46.00, 4, 0); records[5] = new Record("Units in Stock", 39, 25, 17, 5, 0); records[6] = new Record("Discontinued", true, false, true, 6, 0); records[7] = new Record("Last Order", new DateTime(2010, 12, 14), new DateTime(2010, 7, 20), new DateTime(2010, 1, 7), 7); records[8] = new Record("Relevance", 70, 90, 50, 8); records[9] = new Record("Contact Name", "Shelley Burke", "Robb Merchant", "Sven Petersen", 9, 2); records[10] = new Record("Phone", "(100)555-4822", "(111)555-1222", "(120)555-1154", 10, 2); treeList1.DataSource = records; treeList1.ExpandAll(); }
进一步查看Record的源码,发现其构造函数为:
View Code
public Record(string category, object product1, object product2, object product3, int id) : this(category, product1, product2, product3, id, -1) {} public Record(string category, object product1, object product2, object product3, int id, int parentID) { this.fCategory = category; this.fProduct1 = product1; this.fProduct2 = product2; this.fProduct3 = product3; this.fId = id; this.fParentID = parentID; }
在这里需要重视id和parentID的构造方法,这两个属性决定的是树的层级关系。另外,感觉好像TreeList能够自动绑定名称为ID和ParentID的字段,很神奇。
其次,有了数据源就需要设置其Cell的Edit的形式了,静态Edit需要实现的是GetCustomNodeCellEdit这个事件响应,源码如下:
View Code
private void treeList1_GetCustomNodeCellEdit(object sender, DevExpress.XtraTreeList.GetCustomNodeCellEditEventArgs e) { if(e.Column.FieldName != "Category") { object obj = e.Node.GetValue(0); if(obj != null) { switch(obj.ToString()) { case "Category": e.RepositoryItem = repositoryImageComboBox1; break; case "Supplier": e.RepositoryItem = repositoryItemComboBox1; break; case "Unit Price": e.RepositoryItem = repositoryItemCalcEdit1; break; case "Units in Stock": e.RepositoryItem = repositoryItemSpinEdit1; break; case "Discontinued": e.RepositoryItem = repositoryItemCheckEdit1; break; case "Last Order": e.RepositoryItem = repositoryItemDateEdit1; break; case "Relevance": e.RepositoryItem = repositoryItemProgressBar1; break; case "Phone": e.RepositoryItem = repositoryItemTextEdit1; break; } } } }
从源码中可以看出,根据数据源中的名称属性决定某一列的Cell采用哪种编辑控件。不过,这些控件首先都需要在TreeList控件中进行声明的。关于如何声明,请看下图:
二 动态Edit
现在我需要在鼠标点击某个Cell的时候确定当前的Cell绑定的控件类型,这里需要实现TreeList的CustomNodeCellEditForEditing的响应事件。
View Code
private void treeList1_CustomNodeCellEditForEditing(object sender, GetCustomNodeCellEditEventArgs e) { //确定点击的是某一个Cell if(e.Column.FieldName == "treeListVal") { Switch(type) { Case type1: e.RepositoryItem = repositoryItemPopupContainerEditNumber;break; case type2: e.RepositoryItem =repositoryItemPopupContainerEditBelong; break; } } }
RepositoryItem所需要控件需要提前在TreeList控件中进行声明的。