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

  

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

  

先看效果:

  

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

  

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

  

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

  

1。你需要添加列

  

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

  

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

  

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

  

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

  

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

  

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

        & lt;设备名称=" hidc - 1600电视_192.168.230.188”ItemType=癉VR”类型=癘nvif”类型ID="代码="位置==" ID="描述" UniqueID=?92.168.230.188”比;   & lt; IP值=" https://www.yisu.com/zixun/192.168.230.188 "/比;   & lt;端口值=" https://www.yisu.com/zixun/80 "/比;   & lt;用户名值=" https://www.yisu.com/zixun/admin "/比;   & lt;密码值=" https://www.yisu.com/zixun/1234 "/比;   & lt; AuthenAddress值=" https://www.yisu.com/"/比;   & lt; AuthenMode值=" https://www.yisu.com/zixun/1 "/比;   & lt; OnvifUser值=" https://www.yisu.com/zixun/admin "/比;   & lt; OnvifPwd值=" https://www.yisu.com/zixun/1234 "/比;   & lt; OnvifAddress值=" https://www.yisu.com/onvif/device_service "/比;   & lt; RTSPUser值=" https://www.yisu.com/zixun/admin "/比;   & lt; RTSPPwd值=" https://www.yisu.com/zixun/1234 "/比;   & lt; ChildDevices>   & lt;设备名称=" " ItemType=巴ǖ馈崩嘈?袄嘈蚷d="代码="位置==" id="描述" UniqueID=薄氨?   & lt; PTZEnable值=" https://www.yisu.com/zixun/True "/比;   & lt; PTZ1值=" https://www.yisu.com/zixun/5 "/比;   & lt; PTZ2值=" https://www.yisu.com/zixun/15 "/比;   & lt; PTZ3值=" https://www.yisu.com/zixun/25 "/比;   & lt; PTZ4值=" https://www.yisu.com/zixun/35 "/比;   & lt; PTZ5值=" https://www.yisu.com/zixun/45 "/比;   & lt; PTZ6值=" https://www.yisu.com/zixun/55 "/比;   & lt; PTZ7值=" https://www.yisu.com/zixun/65 "/比;   & lt; PTZ8值=" https://www.yisu.com/zixun/75 "/比;   & lt; PTZ9值=" https://www.yisu.com/zixun/85 "/比;   & lt; ChildDevices>   & lt;设备名称=" " ItemType=癛Streamer”类型=袄嘈蚷d=?”码="位置==" id="描述"比;   & lt; MediaProfile值=" https://www.yisu.com/zixun/1 "/比;   & lt;多播值=" https://www.yisu.com/zixun/False "/比;   & lt;/Device>   & lt;设备名称=" " ItemType=癛Streamer”类型=袄嘈蚷d=?”代码="位置==" id="描述"比;   & lt; MediaProfile值=" https://www.yisu.com/zixun/2 "/比;   & lt;多播值=" https://www.yisu.com/zixun/False "/比;   & lt;/Device>   & lt;/ChildDevices>   & lt;/Device>   & lt;/ChildDevices>   & lt;/Device>      

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

        公共空间LoadXmlTree (string xml)   {   XDocument xDoc=XDocument.Parse (xml);   TreeListViewItem项=new TreeListViewItem ();   字符串标题=xDoc.Root.Attribute(“名字”)& # 63;。价值& # 63;& # 63;xDoc.Root.Name.LocalName;   项。文本=标题;   项。ImageIndex=0;   item.SubItems.Add (xDoc.Root.Attribute (UniqueID) & # 63; value);   item.SubItems.Add (xDoc.Root.Attribute (ItemType) & # 63; value);   PopulateTree (xDoc。根,item.Items);   tvDevice.Items.Add(项);   }   公共空间PopulateTree (XElement元素,TreeListViewItemCollection项目)   {   foreach (XElement节点element.Nodes ())   {   TreeListViewItem项=new TreeListViewItem ();   字符串标题=node.Name.LocalName.Trim ();   项。文本=标题;   如果(标题==吧璞浮?   {   var attr=node.Attribute (ItemType) & # 63; value;   开关(attr)   {   例“通道”:项目。ImageIndex=1;打破;   例“RStreamer”:项目。ImageIndex=3;打破;   默认值:休息;   }   item.SubItems.Add (node.Attribute (UniqueID) & # 63; value);   item.SubItems.Add (node.Attribute (ItemType) & # 63; value);   }   其他的   {   项。ImageIndex=2;   item.SubItems.Add (node.Attribute(“价值”)& # 63;Value);   }   如果(node.HasElements)   {   PopulateTree(节点,item.Items);   }   items.Add(项);   }   }

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