Last active
June 1, 2016 22:05
-
-
Save sleimanzublidi/5673170 to your computer and use it in GitHub Desktop.
TreeView - NSOutlineViewDataSource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TreeViewDataSource : NSOutlineViewDataSource | |
{ | |
private readonly List<TreeItem> source = new List<TreeItem>(); | |
public TreeViewDataSource() | |
{} | |
public void Add(TreeItem item) | |
{ | |
source.Add(item); | |
} | |
public void Add(IEnumerable<TreeItem> items) | |
{ | |
source.AddRange(items); | |
} | |
public override int GetChildrenCount(NSOutlineView outlineView, NSObject item) | |
{ | |
if (item == null) return source.Count; | |
return (item as TreeItem).Children.Count; | |
} | |
public override bool ItemExpandable(NSOutlineView outlineView, NSObject item) | |
{ | |
if(item == null) return false; | |
return (item as TreeItem).Children.Count > 0; | |
} | |
public override NSObject GetChild(NSOutlineView outlineView, int childIndex, NSObject item) | |
{ | |
if (item == null) | |
{ | |
return source[childIndex]; | |
} | |
return (item as TreeItem).Children[childIndex]; | |
} | |
public override NSObject GetObjectValue(NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item) | |
{ | |
if (item != null) | |
{ | |
return (NSString)item.ToString(); | |
} | |
return (NSString)"Test"; | |
} | |
} | |
public class TreeItem : NSObject, IEnumerable<TreeItem>, IEnumerable | |
{ | |
public TreeItem(string caption) | |
{ | |
Caption = caption; | |
Children = new List<TreeItem>(); | |
} | |
public string Caption { get; set; } | |
public List<TreeItem> Children { get; set; } | |
public void Add(TreeItem child) | |
{ | |
Children.Add(child); | |
} | |
public override string ToString() | |
{ | |
return Caption; | |
} | |
#region IEnumerable | |
IEnumerator<TreeItem> IEnumerable<TreeItem>.GetEnumerator() | |
{ | |
return Children.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return Children.GetEnumerator(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment