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è)備樹(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)展開(kāi)的方式,設(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開(kāi)源你也可以根據(jù)自己的需要進(jìn)行修改。
總結(jié)
以上所述是小編給大家介紹的c# winform treelistview的使用(treegridview),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- C#中WPF ListView綁定數(shù)據(jù)的實(shí)例詳解
- C# WPF ListView控件的實(shí)例詳解
- C# ListView 點(diǎn)擊表頭對(duì)數(shù)據(jù)進(jìn)行排序功能的實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)在listview中插入圖片實(shí)例代碼
- C#中ListView控件實(shí)現(xiàn)窗體代碼
- C#下listview如何插入圖片
- C#實(shí)現(xiàn)listview Group收縮擴(kuò)展的方法
- C#實(shí)現(xiàn)帶進(jìn)度條的ListView
- C#實(shí)現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法
- 一文掌握C# ListView控件的用法和示例代碼
相關(guān)文章
C#三種判斷數(shù)據(jù)庫(kù)中取出的字段值是否為空(NULL) 的方法
最近操作數(shù)據(jù)庫(kù),需要判斷返回的字段值是否為空,在網(wǎng)上收集了3種方法供大家參考2013-04-04
C# Partial:分部方法和分部類(lèi)代碼實(shí)例
這篇文章主要介紹了C# Partial:分部方法和分部類(lèi)代碼實(shí)例,本文直接給出代碼實(shí)現(xiàn),需要的朋友可以參考下2015-03-03
C#?使用原生?System.IO.Compression?實(shí)現(xiàn)?zip?的壓縮與解壓
這篇文章主要介紹了C#?使用原生?System.IO.Compression?實(shí)現(xiàn)?zip?的壓縮與解壓,zip?是一個(gè)非常常見(jiàn)的壓縮包格式,本文主要用于說(shuō)明如何使用代碼?文件或文件夾壓縮為?zip壓縮包及其解壓操作,需要的朋友可以參考下2022-09-09
Unity實(shí)現(xiàn)卡拉OK歌詞過(guò)渡效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)卡拉OK歌詞過(guò)渡效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
如何利用現(xiàn)代化C#語(yǔ)法簡(jiǎn)化代碼
這篇文章主要給大家介紹了關(guān)于如何利用現(xiàn)代化C#語(yǔ)法簡(jiǎn)化代碼的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
C# 創(chuàng)建、部署和調(diào)用WebService簡(jiǎn)單示例

