Here is my code:
using System.Collections.Generic;using System.ComponentModel;using System.Windows.Forms;namespace infragistic_tree{ public partial class Form1 : Form { public Form1() { InitializeComponent(); var folder1 = new Folder { Name = "Folder1" }; var folder2 = new Folder { Name = "Folder2" }; var folder3 = new Folder { Name = "Folder3" }; var subFolders = new FolderList { folder2 }; folder1.SubFolders = subFolders; var file = new File { Name = "File" }; var files = new BindingList<File> { file }; folder1.Files = files; var subSubFolders = new FolderList { folder3 }; folder2.SubFolders = subSubFolders; var allFolders = new FolderList { folder1 }; ultraTree.DataSource = allFolders; } } public class FolderList : BindingList<Folder> { public FolderList() { } public FolderList(IList<Folder> list) : base(list) { } } public class Folder { public Folder() { } public string Name { get; set; } public FolderList SubFolders { get; set; } public BindingList<File> Files { get; set; } } public class File { public string Name { get; set; } }}
The tree looks like this :
:
I need to have files and folders in the same level, without files and subfolders elements. Another problem is that I do not want to see + sign for the files (and maybe for empty folders).
I think in this case what you want to do is define a base class that Files and Folders both derive from (let's call it 'FileSystemObject'), then bind the control to a BindingList<FileSystemObject>. The 'Folder' class would expose collection properties named 'Folders' and 'Files', but the Files class doesn't, so Files and Folders can reside in the same collection, but where Folder nodes might have children, File nodes would not.
Here is the new version:
using System.Collections.Generic;using System.ComponentModel;using System.Windows.Forms;namespace infragistic_tree{ public partial class Form1 : Form { public Form1() { InitializeComponent(); var folder1 = new Folder {Name = "Folder1"}; var folder2 = new Folder {Name = "Folder2"}; var folder3 = new Folder {Name = "Folder3"}; var file = new File { Name = "File" }; var subItems = new FolderItemsList {folder2, file}; folder1.SubItems = subItems; var subSubFolders = new FolderItemsList {folder3}; folder2.SubItems = subSubFolders; var allFolders = new FolderItemsList {folder1}; ultraTree.DataSource = allFolders; } } public class FolderItemsList : BindingList<FileSystemObject> { public FolderItemsList(){} public FolderItemsList(IList<FileSystemObject> list): base(list){} } public class Folder:FileSystemObject { public Folder(){} public override string Name { get; set; } public override BindingList<FileSystemObject> SubItems { get; set; } } public class File : FileSystemObject { public File(){} public override string Name { get; set; } public override BindingList<FileSystemObject> SubItems { get { return null; } set { } } } public class FileSystemObject { public virtual string Name { get; set; } public virtual BindingList<FileSystemObject> SubItems { get; set; } }}I need to have different icons for folders and for files. Is there a way to do that?