Vikas Mathur
Vikas Mathur

Reputation: 3

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.

Upvotes: 0

Views: 134

Answers (2)

Chris Hamons
Chris Hamons

Reputation: 1509

Fundamentally, Cocoa does not care about your internal data structures, it only cares about two things:

  • Structure of the tree. You supply this via your NSOutlineViewDataSource derived class answering GetChildrenCount / GetChild / ItemExpandable

  • View for each tree. You supply this via your NSOutlineViewDelegate derived class returning one via GetView (with the NSObject you returned in GetChild passed to you).

So your delegate/datasource can be passed a reference to your lists, determine from them how many items/children should be shown, and answer the questions correctly.

You can see a detailed example here: https://github.com/xamarin/mac-samples/blob/master/NSOutlineViewAndTableView/NSOutlineViewAndTableViewExample/NSOutlineViewCode/NSOutlineViewExample.cs#L47

Upvotes: 0

svn
svn

Reputation: 1408

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; }

}

Upvotes: 0

Related Questions