c# winform treelistview的使用(treegridview)實(shí)例詳解
TreeView控件顯示的內(nèi)容比較單一,如果需要呈現(xiàn)更詳細(xì)信息TreeListView是一個(gè)不錯(cuò)的選擇。
先看效果:
首先需要引用文件System.Windows.Forms.TreeListView.dll、System.Runtime.InteropServices.APIs.dll
你可以將TreeListView加入到工具箱中然后在添加到窗體中。
1.你需要添加列
2.你需要添加一個(gè)ImageList作為節(jié)點(diǎn)圖標(biāo)的容器(你還需要配置TreeListView的SmallImageList屬性為ImageList控件的ID)
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 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); } }
說(shuō)明:
TreeListViewItem可構(gòu)造傳入value和imageindex,其中value會(huì)賦值給Text屬性,imageindex就是節(jié)點(diǎn)顯示的圖標(biāo)所對(duì)應(yīng)的ImageList的索引。TreeListViewItem的SubItems就是其擴(kuò)展列,它會(huì)按順序依次顯示到后面的列中。
你可以設(shè)置ExpandMethod屬性來(lái)控制節(jié)點(diǎn)展開的方式,設(shè)置CheckBoxes屬性控制是否顯示復(fù)選框。
你可以通過(guò)訂閱BeforeExpand、BeforeCollapse、BeforeLabelEdit三個(gè)事件來(lái)修改不同狀態(tài)下的圖標(biāo),如:
private void treeListView1_BeforeExpand(object sender, TreeListViewCancelEventArgs e) { if(e.Item.ImageIndex == 0) e.Item.ImageIndex = 1; }
你可以設(shè)置LabelEdit屬性來(lái)激活或禁用編輯,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),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
上一篇:C#實(shí)現(xiàn)泛型List分組輸出元素的方法
欄 目:C#教程
下一篇:C#程序員統(tǒng)計(jì)自己的代碼行數(shù)
本文標(biāo)題:c# winform treelistview的使用(treegridview)實(shí)例詳解
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5353.html
您可能感興趣的文章
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#停止線程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置