Search code examples
nsoutlineviewxamarin.mac

How to populate NSOutlineView in Xamarin Mac having 2 different nested Lists


I am developing a application in Xamarin for Mac which requires a TreeView. I am implementing NSOutlineView to achieving TreeView. I want to populate the outline view from a list which itself contains a list of different type. for example the List is List list The Definitions of classes are as follow

Class ClassA:NSObject
{
   public List<ClassB> listClassB { get; set; }
   public string Types { get; set; }
}

And ClassB is as follow

public class ClassB
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int TypeName { get; set; }
        public ClassC Owner { get; set; }
        public DateTime LastActivity { get; set; }
        public Collection<ClassC> Users { get; set; }
    }

But I not getting how to implement this list in NSOutlineView in Xamarin for Mac. The examples given by xamarin contains only one class. but I have 3 classes

Please help me as soon as possible.


Solution

  • There are several ways to solve this. I use a single generic class as sourcelistitem which has a children list of generic items to be able to build the tree.

    You can either make you classes inherit from the generic sourcelistitem or create properties in the generic item class to it can contain of your different classes.

    Simplified example with inheritance method:

    public class SourceListItem: NSObject, IEnumerator, IEnumerable
    {
        private List<SourceListItem> _items = new List<SourceListItem> ();
    }
    
    Class ClassA: SourceListItem
    {
    
       public string Types { get; set; }
    }
    
    Class ClassB: SourceListItem
    {
    
      public int Id { get; set; }
      public string Name { get; set; }
      public int TypeName { get; set; }
      public DateTime LastActivity { get; set; }
    
    }