void UpdateSelection() { var rowsToSelect = new HashSet <nuint>(); void DiscoverSelected(ITreeNode node) // todo: remove linear traversal { if (node.IsSelected) { var rowToSelect = treeView.RowForItem(ToObject(nodeToItem[node])); Debug.Assert(rowToSelect >= 0); rowsToSelect.Add((nuint)rowToSelect); } if (node.IsExpanded) { foreach (var n in node.Children) { DiscoverSelected(n); } } } DiscoverSelected(rootItem.Node); treeView.SelectRows(NSIndexSet.FromArray(rowsToSelect.ToArray()), byExtendingSelection: false); }
public static void SelectAndScrollInView <Item>(NSOutlineView treeView, Item[] items, Func <Item, Item> parentGetter) where Item : NSObject { var rows = new List <uint> (); foreach (var item in items) { var rowIdx = treeView.RowForItem(item); if (rowIdx < 0) { var stack = new Stack <Item>(); for (var i = parentGetter(item); i != null; i = parentGetter(i)) { stack.Push(i); } while (stack.Count > 0) { treeView.ExpandItem(stack.Pop()); } rowIdx = treeView.RowForItem(item); } if (rowIdx >= 0) { rows.Add((uint)rowIdx); } } treeView.SelectRows( NSIndexSet.FromArray(rows.ToArray()), byExtendingSelection: false ); if (rows.Count > 0) { treeView.ScrollRowToVisible((nint)rows[0]); } }