I cant seem to figure out how to create a simple NSOutlineView with 2 columns, and a datastructure that is more than 1 level deep (a hierachy).
I’ve been researching this for days, and all I can find is Objective C examples, which I really can’t use for anything.
I understand there are different patterns for doing this, one being the DataSource pattern. I tried creating a class that inherited from NSOutlineViewDataSource, however thats all I got, I have no clue on what I should do next!
Lets say I would like to display the following class in my NSOutlineView:
public class Person
{
public string Name {get;set;} // First column
public int Age {get;set} // Second column
public List<Person> Children {get;set} // Children
}
What would be the most trivial approach to accomplishing this?
Brace yourselves… A level-independant NSOutlineView in MonoMac!
After hundreds of google searches, and looking through ObjC as well as C# code, I finally figured out how to do it! I will post my solution here, in case someone else needs it.
This may or may not be the best way to do it, but it works for me.
Step 1: In Interface Builder, add an NSOutlineView. Add 2 columns to it, and set their Identifier to
colName, andcolAge.Also, while you’re at it, add a button to your form.
Step 2: Create an outlet for the NSOutlineView – I called mine
lvMainbecause I come from a VCL background. Also, create an action for your button (this will be the onClick handler).Step 3: Save your XIB file, and return to Mono – it will update your project file. Now, we want to create the model we wish to use for our view.
For this example, I will use a simple Person object:
Nothing overly complicated there.
Step 4: Create the datasource. For this example, this is what I made:
Step 5 – The best step: Bind the datasource and add dummy data! We also wanna refresh our view each time we add a new element. This can probably be optimized, but I’m still in the “Oh my god its working” zone, so I currently don’t care.
I hope this information can help the troubled souls of the MonoMac newcomers like myself.