TreeList 控件添加节点或绑定数据源前,需要对其进行初始化(否则就会出现,第一次添加数据不显示,再次添加就会显示正常的问题)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
this.treeList1.BeginInit();
//绑定图片列表
treeList1.SelectImageList = imageCollection1;
this.treeList1.Columns.Clear();
//添加显示列
this.treeList1.Columns.Add(new TreeListColumn { FieldName = "name", Caption = "图层", Width = 160, VisibleIndex = 1 });
int count = treeList1.Columns.Count;
//设置树控件的层次关系及属性
treeList1.KeyFieldName = "name";
treeList1.ParentFieldName = "pid";
treeList1.ImageIndexFieldName = "imgid";
//只读
this.treeList1.OptionsBehavior.Editable = false;
this.treeList1.OptionsView.EnableAppearanceOddRow = true;
this.treeList1.OptionsView.EnableAppearanceEvenRow = true;
//不允许拖动
this.treeList1.OptionsDragAndDrop.DragNodesMode = DragNodesMode.None;
treeList1.EndInit();

TreeList 添加数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
treeList1.BeginUpdate();
//数据源 DataTable
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("pid", typeof(int));
dt.Columns.Add("imgid", typeof(int));

//从 xml 文件中读取相关配置
XDocument xDocument = XDocument.Load(Application.StartupPath + "\\SymbolizationConfig.xml");
XElement mapxEle = null;
XElement mapEle = null;
IEnumerable<XElement> enumerable = xDocument.Root.Elements();
foreach (var item in enumerable)
{
if (comboBoxEdit_Mapx.Text.Equals(item.Attribute("meaning").Value))
{
mapxEle = item;
break;
}
}
enumerable = mapxEle.Elements();
foreach (var item in enumerable)
{
if (comboBoxEdit_Map.Text.Equals(item.Attribute("meaning").Value))
{
mapEle = item;
break;
}
}
enumerable = mapEle.Elements();
foreach (var item in enumerable)
{
List<string> typeLst = new List<string>() { "PNT", "LIN", "ARE", "ANN" };
string name = item.Attribute("name").Value;
string type = item.Attribute("sfclstype").Value;
int imgidx = typeLst.IndexOf(type);

DataRow dr = dt.NewRow();
dr["name"] = name;
dr["pid"] = -1;
dr["imgid"] = imgidx;
dt.Rows.Add(dr);
}
//绑定数据源
treeList1.DataSource = dt;
treeList1.EndUpdate();

节点添加独立的右键菜单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void treeList_Doc_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Point point = new Point(Cursor.Position.X, Cursor.Position.Y);
TreeList tree = (TreeList)sender;
TreeListHitInfo hitInfo = tree.CalcHitInfo(e.Location);
if (hitInfo.HitInfoType != HitInfoType.Cell) return;
tree.SetFocusedNode(hitInfo.Node);
TreeListNode node = tree.FocusedNode;
if (node != null && node.Level == 2)
{
popupMenu_Layer.ShowPopup(point);
}
}
}