Table of Contents

Class TreeView<T>

Namespace
Terminal.Gui
Assembly
Terminal.Gui.dll

Hierarchical tree view with expandable branches. Branch objects are dynamically determined when expanded using a user defined ITreeBuilder<T>.

See TreeView Deep Dive for more information.

public class TreeView<T> : View, IDisposable, ISupportInitializeNotification, ISupportInitialize, ITreeView where T : class

Type Parameters

T
Inheritance
TreeView<T>
Implements
Derived
Inherited Members

Constructors

TreeView()

Creates a new tree view with absolute positioning. Use AddObjects(IEnumerable<T>) to set set root objects for the tree. Children will not be rendered until you set TreeBuilder.

TreeView(ITreeBuilder<T>)

Initialises TreeBuilder.Creates a new tree view with absolute positioning. Use AddObjects(IEnumerable<T>) to set set root objects for the tree.

Fields

Filter

Interface for filtering which lines of the tree are displayed e.g. to provide text searching. Defaults to null (no filtering).

NoBuilderError

Error message to display when the control is not properly initialized at draw time (nodes added but no tree builder set).

Properties

AllowLetterBasedNavigation

True makes a letter key press navigate to the next visible branch that begins with that letter/digit.

AspectGetter

Returns the string representation of model objects hosted in the tree. Default implementation is to call ToString().

ColorGetter

Delegate for multi colored tree views. Return the ColorScheme to use for each passed object or null to use the default.

ContentHeight

The current number of rows in the tree (ignoring the controls bounds).

DesiredCursorVisibility

Get / Set the wished cursor when the tree is focused. Only applies when MultiSelect is true. Defaults to Invisible.

KeystrokeNavigator

Gets the CollectionNavigator that searches the Objects collection as the user types.

MaxDepth

Maximum number of nodes that can be expanded in any given branch.

MultiSelect

True to allow multiple objects to be selected at once.

ObjectActivationButton

Mouse event to trigger ObjectActivated. Defaults to double click (Button1DoubleClicked). Set to null to disable this feature.

ObjectActivationKey

Key which when pressed triggers ObjectActivated. Defaults to Enter.

Objects

The root objects in the tree, note that this collection is of root objects only.

ScrollOffsetHorizontal

The amount of tree view that has been scrolled to the right (horizontally).

ScrollOffsetVertical

The amount of tree view that has been scrolled off the top of the screen (by the user scrolling down).

SelectedObject

The currently selected object in the tree. When MultiSelect is true this is the object at which the cursor is at.

Style

Contains options for changing how the tree is rendered.

TreeBuilder

Determines how sub branches of the tree are dynamically built at runtime as the user expands root nodes.

Methods

ActivateSelectedObjectIfAny()

Triggers the ObjectActivated event with the SelectedObject.

This method also ensures that the selected object is visible.

AddObject(T)

Adds a new root level object unless it is already a root of the tree.

AddObjects(IEnumerable<T>)

Adds many new root level objects. Objects that are already root objects are ignored.

AdjustSelection(int, bool)

The number of screen lines to move the currently selected object by. Supports negative values. offset. Each branch occupies 1 line on screen.

AdjustSelectionToBranchEnd()

Moves the selection to the last child in the currently selected level.

AdjustSelectionToBranchStart()

Moves the selection to the first child in the currently selected level.

AdjustSelectionToNextItemBeginningWith(char, StringComparison)

Moves the SelectedObject to the next item that begins with character.

This method will loop back to the start of the tree if reaching the end without finding a match.

CanExpand(T)

Returns true if the given object o is exposed in the tree and can be expanded otherwise false.

ClearObjects()

Removes all objects from the tree and clears SelectedObject.

Collapse()

Collapses the SelectedObject

Collapse(T)

Collapses the supplied object if it is currently expanded .

CollapseAll()

Collapses all root nodes in the tree.

CollapseAll(T)

Collapses the supplied object if it is currently expanded. Also collapses all children branches (this will only become apparent when/if the user expands it again).

CollapseImpl(T, bool)

Implementation of Collapse(T) and CollapseAll(T). Performs operation and updates selection if disapeared.

CursorLeft(bool)

Determines systems behaviour when the left arrow key is pressed. Default behaviour is to collapse the current tree node if possible otherwise changes selection to current branches parent.

EnsureVisible(T)

Adjusts the ScrollOffsetVertical to ensure the given model is visible. Has no effect if already visible.

Expand()

Expands the currently SelectedObject.

Expand(T)

Expands the supplied object if it is contained in the tree (either as a root object or as an exposed branch object).

ExpandAll()

Fully expands all nodes in the tree, if the tree is very big and built dynamically this may take a while (e.g. for file system).

ExpandAll(T)

Expands the supplied object and all child objects.

GetAllSelectedObjects()

Returns SelectedObject (if not null) and all multi selected objects if MultiSelect is true

GetChildren(T)

Returns the currently expanded children of the passed object. Returns an empty collection if the branch is not exposed or not expanded.

GetContentWidth(bool)

Returns the maximum width line in the tree including prefix and expansion symbols.

GetObjectOnRow(int)

Returns the object in the tree list that is currently visible. at the provided row. Returns null if no object is at that location. If you have screen coordinates then use ScreenToView(int, int) to translate these into the client area of the TreeView<T>.

GetObjectRow(T)

Returns the Y coordinate within the Bounds of the tree at which toFind would be displayed or null if it is not currently exposed (e.g. its parent is collapsed).

Note that the returned value can be negative if the TreeView is scrolled down and the toFind object is off the top of the view.

GetParent(T)

Returns the parent object of o in the tree. Returns null if the object is not exposed in the tree.

GetScrollOffsetOf(T)

Returns the index of the object o if it is currently exposed (it's parent(s) have been expanded). This can be used with ScrollOffsetVertical and SetNeedsDisplay() to scroll to a specific object.

GoTo(T)

Changes the SelectedObject to toSelect and scrolls to ensure it is visible. Has no effect if toSelect is not exposed in the tree (e.g. its parents are collapsed).

GoToEnd()

Changes the SelectedObject to the last object in the tree and scrolls so that it is visible.

GoToFirst()

Changes the SelectedObject to the first root object and resets the ScrollOffsetVertical to 0.

InvalidateLineMap()

Clears any cached results of the tree state.

IsExpanded(T)

Returns true if the given object o is exposed in the tree and expanded otherwise false.

IsSelected(T)

Returns true if the model is either the SelectedObject or part of a MultiSelect.

MouseEvent(MouseEvent)

Method invoked when a mouse event is generated

MovePageDown(bool)

Moves the selection down by the height of the control (1 page).

MovePageUp(bool)

Moves the selection up by the height of the control (1 page).

OnEnter(View)

Method invoked when a view gets focus.

OnObjectActivated(ObjectActivatedEventArgs<T>)

Raises the ObjectActivated event.

OnSelectionChanged(SelectionChangedEventArgs<T>)

Raises the SelectionChanged event.

PositionCursor()

Positions the cursor at the start of the selected objects line (if visible).

ProcessKey(KeyEvent)

If the view is focused, gives the view a chance to process the keystroke.

RebuildTree()

Rebuilds the tree structure for all exposed objects starting with the root objects. Call this method when you know there are changes to the tree but don't know which objects have changed (otherwise use RefreshObject(T, bool)).

Redraw(Rect)

Redraws this view and its subviews; only redraws the views that have been flagged for a re-display.

RefreshObject(T, bool)

Refreshes the state of the object o in the tree. This will recompute children, string representation etc.

Remove(T)

Removes the given root object from the tree

ScrollDown()

Scrolls the view area down a single line without changing the current selection.

ScrollUp()

Scrolls the view area up a single line without changing the current selection.

SelectAll()

Selects all objects in the tree when MultiSelect is enabled otherwise does nothing.

Events

ObjectActivated

This event is raised when an object is activated e.g. by double clicking or pressing ObjectActivationKey.

SelectionChanged

Called when the SelectedObject changes.