原創(chuàng)|其它|編輯:郝浩|2012-10-11 16:04:50.000|閱讀 835 次
概述:所謂Edit樹節(jié)點的列,有如下兩層含義:(1)根據(jù)某標識決定節(jié)點的某列Cell的形式,例如Cell可選擇為時間控件,DropDownList控件等等;(2)與第一點類似,但是需要在運行時才動態(tài)決定Cell的類型,例如鼠標單擊某Cell的時候,決定此Cell為哪種控件。因此將這兩種類型概括為“靜態(tài)Edit”和“動態(tài)Edit”。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
所謂Edit樹節(jié)點的列,有如下兩層含義:(1)根據(jù)某標識決定節(jié)點的某列Cell的形式,例如Cell可選擇為時間控件,DropDownList控件等等;(2)與第一點類似,但是需要在運行時才動態(tài)決定Cell的類型,例如鼠標單擊某Cell的時候,決定此Cell為哪種控件。因此將這兩種類型概括為“靜態(tài)Edit”和“動態(tài)Edit”。
一、靜態(tài)Edit
靜態(tài)Edit在DevExpress的官方示例中有一個很棒的示例。
分析此Demo的源碼,首先看下初始化數(shù)據(jù)部分,函數(shù)為InitData:
//這個函數(shù)構(gòu)造了一個包含11元素的Record數(shù)組,并將此數(shù)組作為TreeList的數(shù)據(jù)源。
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的源碼,發(fā)現(xiàn)其構(gòu)造函數(shù)為:
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的構(gòu)造方法,這兩個屬性決定的是樹的層級關(guān)系。另外,感覺好像TreeList能夠自動綁定名稱為ID和ParentID的字段,很神奇。
其次,有了數(shù)據(jù)源就需要設(shè)置其Cell的Edit的形式了,靜態(tài)Edit需要實現(xiàn)的是GetCustomNodeCellEdit這個事件響應(yīng),源碼如下:
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;
}
}
}
}
從源碼中可以看出,根據(jù)數(shù)據(jù)源中的名稱屬性決定某一列的Cell采用哪種編輯控件。不過,這些控件首先都需要在TreeList控件中進行聲明的。關(guān)于如何聲明,請看下圖:
二、動態(tài)Edit
現(xiàn)在我需要在鼠標點擊某個Cell的時候確定當(dāng)前的Cell綁定的控件類型,這里需要實現(xiàn)TreeList的CustomNodeCellEditForEditing的響應(yīng)事件。
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控件中進行聲明的。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:三夏健-博客園