Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
70
Binding custom class to tree
posted

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).

 

Parents
No Data
Reply
  • 69832
    Suggested Answer
    Offline posted

    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.

Children