c# winform treelistview的使用(treegridview)實例詳解
TreeView控件顯示的內(nèi)容比較單一,如果需要呈現(xiàn)更詳細信息TreeListView是一個不錯的選擇。
先看效果:
首先需要引用文件System.Windows.Forms.TreeListView.dll、System.Runtime.InteropServices.APIs.dll
你可以將TreeListView加入到工具箱中然后在添加到窗體中。
1.你需要添加列
2.你需要添加一個ImageList作為節(jié)點圖標的容器(你還需要配置TreeListView的SmallImageList屬性為ImageList控件的ID)
3.現(xiàn)在可以給控件綁定數(shù)據(jù)了
此控件比較適合呈現(xiàn)具有父子級關(guān)系的復雜數(shù)據(jù)結(jié)構(gòu),當然也包含XML格式的數(shù)據(jù)
下面嘗試解析一個設備樹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 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可構(gòu)造傳入value和imageindex,其中value會賦值給Text屬性,imageindex就是節(jié)點顯示的圖標所對應的ImageList的索引。TreeListViewItem的SubItems就是其擴展列,它會按順序依次顯示到后面的列中。
你可以設置ExpandMethod屬性來控制節(jié)點展開的方式,設置CheckBoxes屬性控制是否顯示復選框。
你可以通過訂閱BeforeExpand、BeforeCollapse、BeforeLabelEdit三個事件來修改不同狀態(tài)下的圖標,如:
private void treeListView1_BeforeExpand(object sender, TreeListViewCancelEventArgs e) { if(e.Item.ImageIndex == 0) e.Item.ImageIndex = 1; }
你可以設置LabelEdit屬性來激活或禁用編輯,TreeListViewBeforeLabelEditEventArgs參數(shù)提供了相應的屬性值。
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ù)自己的需要進行修改。
總結(jié)
以上所述是小編給大家介紹的c# winform treelistview的使用(treegridview),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Unity中的RegisterPlugins實用案例深入解析
這篇文章主要為大家介紹了Unity中的RegisterPlugins實用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05超簡單C#獲取帶漢字的字符串真實長度(單個英文長度為1,單個中文長度為2)
正常情況下,我們是直接去string的length的,但是漢字是有兩個字節(jié)的,所以直接用length是錯的2018-03-03使用MSScriptControl 在 C# 中讀取json數(shù)據(jù)的方法
下面小編就為大家?guī)硪黄褂肕SScriptControl 在 C# 中讀取json數(shù)據(jù)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01c#使用S22.Imap收劍靈激活碼郵件代碼示例(imap收郵件)
一個IMAP收發(fā)郵件的類庫S22.IMAP,方便易用,下面來個例子可以收劍靈激活碼郵件2013-12-12解決unity rotate旋轉(zhuǎn)物體 限制物體旋轉(zhuǎn)角度的大坑
這篇文章主要介紹了解決unity rotate旋轉(zhuǎn)物體 限制物體旋轉(zhuǎn)角度的大坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04C#?WinForm?RichTextBox文本動態(tài)滾動顯示文本方式
這篇文章主要介紹了C#?WinForm?RichTextBox文本動態(tài)滾動顯示文本方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03