当前位置: 移动技术网 > IT编程>开发语言>c# > c# winform treelistview的使用(treegridview)实例详解

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
treeview控件显示的内容比较单一,如果需要呈现更详细信息treelistview是一个不错的选择。 先看效果: 首先需要引用文件system.windows.

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

先看效果:

首先需要引用文件system.windows.forms.treelistview.dll、system.runtime.interopservices.apis.dll

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

1.你需要添加列

2.你需要添加一个imagelist作为节点图标的容器(你还需要配置treelistview的smallimagelist属性为imagelist控件的id)

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

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

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

<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的结构         

 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三个事件来修改不同状态下的图标,如:  

 private void treelistview1_beforeexpand(object sender, treelistviewcanceleventargs e)
    {
      if(e.item.imageindex == 0) e.item.imageindex = 1;
    }

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

 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开源你也可以根据自己的需要进行修改。

总结

以上所述是小编给大家介绍的c# winform treelistview的使用(treegridview),希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网