国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c# winform treelistview的使用(treegridview)實(shí)例詳解

2019-10-29 21:08:05
字體:
供稿:網(wǎng)友

TreeView控件顯示的內(nèi)容比較單一,如果需要呈現(xiàn)更詳細(xì)信息TreeListView是一個(gè)不錯(cuò)的選擇。

先看效果:

winform,treelistview,treegridview

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

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

1.你需要添加列

winform,treelistview,treegridview

2.你需要添加一個(gè)ImageList作為節(jié)點(diǎn)圖標(biāo)的容器(你還需要配置TreeListView的SmallImageList屬性為ImageList控件的ID)

winform,treelistview,treegridview

3.現(xiàn)在可以給控件綁定數(shù)據(jù)了

此控件比較適合呈現(xiàn)具有父子級(jí)關(guān)系的復(fù)雜數(shù)據(jù)結(jié)構(gòu),當(dāng)然也包含XML格式的數(shù)據(jù)

下面嘗試解析一個(gè)設(shè)備樹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的結(jié)構(gòu)         

 public void LoadXmlTree(string xml)    {      XDocument xDoc = XDocument.Parse(xml);      TreeListViewItem item = new TreeListViewItem();      string      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        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可構(gòu)造傳入value和imageindex,其中value會(huì)賦值給Text屬性,imageindex就是節(jié)點(diǎn)顯示的圖標(biāo)所對應(yīng)的ImageList的索引。TreeListViewItem的SubItems就是其擴(kuò)展列,它會(huì)按順序依次顯示到后面的列中。

你可以設(shè)置ExpandMethod屬性來控制節(jié)點(diǎn)展開的方式,設(shè)置CheckBoxes屬性控制是否顯示復(fù)選框。

你可以通過訂閱BeforeExpand、BeforeCollapse、BeforeLabelEdit三個(gè)事件來修改不同狀態(tài)下的圖標(biāo),如:  

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

你可以設(shè)置LabelEdit屬性來激活或禁用編輯,TreeListViewBeforeLabelEditEventArgs參數(shù)提供了相應(yīng)的屬性值。   

 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開源你也可以根據(jù)自己的需要進(jìn)行修改。

總結(jié)

以上所述是小編給大家介紹的c# winform treelistview的使用(treegridview),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青海省| 东宁县| 泗水县| 石门县| 临潭县| 二连浩特市| 五寨县| 南乐县| 尚义县| 承德市| 红桥区| 佛坪县| 临夏县| 徐州市| 杨浦区| 普兰店市| 枞阳县| 砀山县| 台南县| 东莞市| 颍上县| 舞钢市| 昌黎县| 庆云县| 安龙县| 宁蒗| 兰溪市| 潼南县| 元氏县| 南安市| 繁昌县| 浦城县| 北票市| 敦化市| 观塘区| 远安县| 璧山县| 抚州市| 乌拉特前旗| 延边| 东乡族自治县|