本示例展示了如何只使用事件,在 TreeList 控件中动态显示系统中目录和文件的信息。 此示例的完整代码在 XtraTreeList Main Demo 中的 Explorer (Virtual Tree) 模块中可用。 此处只列出了一部分代码,给出 TreeList 事件如何动态加载工作的基础理解。
为了为 TreeList 控件提供数据,接管了 TreeList.VirtualTreeGetChildNodes 和 TreeList.VirtualTreeGetCellValue 事件。 第一个事件用于提供根节点和子节点的列表。 第二个事件用于提供节点单元格的取值。 为了保存由最终用户对节点单元格作出的更改,接管了 TreeList.VirtualTreeSetCellValue 事件。 但是,在此处省略了它的实现。 在本例中,假设 TreeList 控件包含三个列,显示目录/文件名称、目录标记和文件大小。 通过 VirtualTreeGetCellValue 事件来提供数据。
为了支持动态数据加载,TreeList.DataSource 属性必须被设置为任何自定义对象,但除去 IList 对象或实现了 IVirtualTreeListData interface 接口的对象之外。 在本示例中,DataSource 属性值被设置为一个新的 Object 实例。
下面的插图展示了运行结果。
C# | 复制代码 |
---|---|
// Specifies whether the root nodes that represent the local drivers are created. bool loadDrives = false; private void Form1_Load(object sender, System.EventArgs e) { treeList1.DataSource = new object(); } // Indicates whether the processed node corresponds to the file. bool IsFile(DirectoryInfo info){ return (info.Attributes & FileAttributes.Directory) == 0; } // Initializes cell values. private void treeList1_VirtualTreeGetCellValue(object sender, DevExpress.XtraTreeList.VirtualTreeGetCellValueInfo e) { DirectoryInfo di = new DirectoryInfo((string)e.Node); if (e.Column == treeListColumn1) e.CellData = di.Name; if (e.Column == treeListColumn2) { if (!IsFile(di)) e.CellData = "Folder"; else e.CellData = "File"; } if (e.Column == treeListColumn3) { if (IsFile(di)){ e.CellData = new FileInfo((string)e.Node).Length; } else e.CellData = null; } } // Creates and initializes child nodes. private void treeList1_VirtualTreeGetChildNodes(object sender, DevExpress.XtraTreeList.VirtualTreeGetChildNodesInfo e) { if (!loadDrives){ // create drives string[] root = Directory.GetLogicalDrives(); e.Children = root; loadDrives = true; } else { try{ string path = (string)e.Node; if(Directory.Exists(path)){ string[] dirs = Directory.GetDirectories(path); string[] files = Directory.GetFiles(path); string[] arr = new string[dirs.Length + files.Length]; dirs.CopyTo(arr,0); files.CopyTo(arr,dirs.Length); e.Children = arr; } else e.Children = new object[]{}; } catch { e.Children = new object[]{}; } } } private void treeList1_VirtualTreeSetCellValue(object sender, VirtualTreeSetCellValueInfo e) { //... } |
Visual Basic | 复制代码 |
---|---|
Dim loadDrives As Boolean = False Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load TreeList1.DataSource = New Object() End Sub Function IsFile(ByVal info As DirectoryInfo) As Boolean Return (info.Attributes And FileAttributes.Directory) = 0 End Function Private Sub TreeList1_VirtualTreeGetCellValue(ByVal sender As Object, _ ByVal e As DevExpress.XtraTreeList.VirtualTreeGetCellValueInfo) _ Handles TreeList1.VirtualTreeGetCellValue Dim di As New DirectoryInfo(CStr(e.Node)) If e.Column Is TreeListColumn1 Then e.CellData = di.Name End If If e.Column Is TreeListColumn2 Then If Not IsFile(di) Then e.CellData = "Folder" Else e.CellData = "File" End If End If If e.Column Is TreeListColumn3 Then If IsFile(di) Then e.CellData = New FileInfo(CStr(e.Node)).Length Else e.CellData = Nothing End If End If End Sub Private Sub TreeList1_VirtualTreeGetChildNodes(ByVal sender As Object, _ ByVal e As DevExpress.XtraTreeList.VirtualTreeGetChildNodesInfo) _ Handles TreeList1.VirtualTreeGetChildNodes If loadDrives = False Then Dim root As String() = Directory.GetLogicalDrives() e.Children = root loadDrives = True Else Try Dim path As String = CStr(e.Node) If Directory.Exists(path) Then Dim dirs As String() = Directory.GetDirectories(path) Dim files As String() = Directory.GetFiles(path) Dim arr(dirs.Length + files.Length) As String dirs.CopyTo(arr, 0) files.CopyTo(arr, dirs.Length) e.Children = arr Else e.Children = New Object() {} End If Catch End Try End If End Sub Private Sub TreeList1_VirtualTreeSetCellValue(ByVal sender As System.Object, _ ByVal e As DevExpress.XtraTreeList.VirtualTreeSetCellValueInfo) _ Handles TreeList1.VirtualTreeSetCellValue '... End Sub |