page contents

c# winform treelistview的使用(treegridview)实例详解

本文讲述了c# winform treelistview的使用(treegridview)实例详解!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-08-9finFomi6306d7d228d13.png

本文讲述了c# winform treelistview的使用(treegridview)实例详解!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

TreeView控件显示的内容比较单一,如果需要呈现更详细信息TreeListView是一个不错的选择。

先看效果:

首先需要引用文件System.Windows.Forms.TreeListView.dll、System.Runtime.InteropServices.APIs.dll

你可以将TreeListView加入到工具箱中然后在添加到窗体中。

1.你需要添加列

2.你需要添加一个ImageList作为节点图标的容器(你还需要配置TreeListView的SmallImageList属性为ImageList控件的ID)

3.现在可以给控件绑定数据了

此控件比较适合呈现具有父子级关系的复杂数据结构,当然也包含XML格式的数据

下面尝试解析一个设备树XML然后绑定到控件中:

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
<Device name="hidc-1600tv _192.168.230.188" ItemType="DVR" type="Onvif" TypeID="" Code="" location="" Description="" ID="" UniqueID="192.168.230.188">
 <IP Value="192.168.230.188" />
 <Port Value="80" />
 <Username Value="admin" />
 <Password Value="1234" />
 <AuthenAddress Value="/" />
 <AuthenMode Value="1" />
 <OnvifUser Value="admin" />
 <OnvifPwd Value="1234" />
 <OnvifAddress Value="/onvif/device_service" />
 <RTSPUser Value="admin" />
 <RTSPPwd Value="1234" />
 <ChildDevices>
  <Device name="" ItemType="Channel" type="" TypeID="" Code="" location="" Description="" id="" UniqueID="">
   <PTZEnable Value="True" />
   <PTZ1 Value="5" />
   <PTZ2 Value="15" />
   <PTZ3 Value="25" />
   <PTZ4 Value="35" />
   <PTZ5 Value="45" />
   <PTZ6 Value="55" />
   <PTZ7 Value="65" />
   <PTZ8 Value="75" />
   <PTZ9 Value="85" />
   <ChildDevices>
    <Device name="" ItemType="RStreamer" type="" TypeID="1" Code="" location="" Description="" id="">
     <MediaProfile Value="1" />
     <Multicast Value="False" />
    </Device>
    <Device name="" ItemType="RStreamer" type="" TypeID="2" Code="" location="" Description="" id="">
     <MediaProfile Value="2" />
     <Multicast Value="False" />
    </Device>
   </ChildDevices>
  </Device>
 </ChildDevices>
</Device>

使用递归算法很容易提取XML的结构         

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
public void LoadXmlTree(string xml)
   {
     XDocument xDoc = XDocument.Parse(xml);
     TreeListViewItem item = new TreeListViewItem();
     string title = xDoc.Root.Attribute("name")?.Value ?? xDoc.Root.Name.LocalName;
     item.Text = title;
     item.ImageIndex = 0;
     item.SubItems.Add(xDoc.Root.Attribute("UniqueID")?.Value);
     item.SubItems.Add(xDoc.Root.Attribute("ItemType")?.Value);
     PopulateTree (xDoc.Root, item.Items);
     tvDevice.Items.Add(item);
   }
   public void PopulateTree (XElement element, TreeListViewItemCollection items)
   {
     foreach (XElement node in element.Nodes())
     {
       TreeListViewItem item = new TreeListViewItem();
       string title = node.Name.LocalName.Trim();
       item.Text = title;
       if (title == "Device")
       {
         var attr = node.Attribute("ItemType")?.Value;
         switch (attr)
         {
           case "Channel": item.ImageIndex = 1; break;
           case "RStreamer": item.ImageIndex = 3; break;
           default: break;
         }
         item.SubItems.Add(node.Attribute("UniqueID")?.Value);
         item.SubItems.Add(node.Attribute("ItemType")?.Value);
       }
       else
       {
         item.ImageIndex = 2;
         item.SubItems.Add(node.Attribute("Value")?.Value);
       }
       if (node.HasElements)
       {
         PopulateTree (node, item.Items);
       }
       items.Add(item);
     }
   }

说明:

TreeListViewItem可构造传入value和imageindex,其中value会赋值给Text属性,imageindex就是节点显示的图标所对应的ImageList的索引。TreeListViewItem的SubItems就是其扩展列,它会按顺序依次显示到后面的列中。

你可以设置ExpandMethod属性来控制节点展开的方式,设置CheckBoxes属性控制是否显示复选框。

你可以通过订阅BeforeExpand、BeforeCollapse、BeforeLabelEdit三个事件来修改不同状态下的图标,如:  

1
2
3
4
private void treeListView1_BeforeExpand(object sender, TreeListViewCancelEventArgs e)
   {
     if(e.Item.ImageIndex == 0) e.Item.ImageIndex = 1;
   }

你可以设置LabelEdit属性来激活或禁用编辑,TreeListViewBeforeLabelEditEventArgs参数提供了相应的属性值。   

1
2
3
4
5
6
7
8
9
private void treeListView1_BeforeLabelEdit(object sender, TreeListViewBeforeLabelEditEventArgs e)
   {
     if(e.ColumnIndex == 1)
     {
       ComboBox combobox = new ComboBox();
       combobox.Items.AddRange(new string[]{"Html","Css","Javascript"});
       e.Editor = combobox;
     }
   }

TreeListView开源你也可以根据自己的需要进行修改。

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2022-08-25 10:02
  • 阅读 ( 931 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1324 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章