Table of Contents

Class TreeView

Namespace
Avalonia.Controls
Assembly
Avalonia.Controls.dll

Displays a hierarchical tree of data.

public class TreeView : ItemsControl, INotifyPropertyChanged, IDataContextProvider, ILogical, IThemeVariantHost, IResourceHost, IResourceNode, IStyleHost, ISetLogicalParent, ISetInheritanceParent, ISupportInitialize, IStyleable, INamed, IInputElement, IDataTemplateHost, ISetterValue, IChildIndexProvider, ICustomKeyboardNavigation
Inheritance
TreeView
Implements
Inherited Members
Extension Methods

Constructors

TreeView()

public TreeView()

Fields

AutoScrollToSelectedItemProperty

Defines the AutoScrollToSelectedItem property.

public static readonly StyledProperty<bool> AutoScrollToSelectedItemProperty

Field Value

StyledProperty<bool>

SelectedItemProperty

Defines the SelectedItem property.

public static readonly DirectProperty<TreeView, object?> SelectedItemProperty

Field Value

DirectProperty<TreeView, object>

SelectedItemsProperty

Defines the SelectedItems property.

public static readonly DirectProperty<TreeView, IList> SelectedItemsProperty

Field Value

DirectProperty<TreeView, IList>

SelectionModeProperty

Defines the SelectionMode property.

public static readonly StyledProperty<SelectionMode> SelectionModeProperty

Field Value

StyledProperty<SelectionMode>

Properties

AutoScrollToSelectedItem

Gets or sets a value indicating whether to automatically scroll to newly selected items.

public bool AutoScrollToSelectedItem { get; set; }

Property Value

bool

Remarks

This property is of limited use with TreeView as it will only scroll to realized items. To scroll to a non-expanded item, you need to ensure that its ancestors are expanded.

ItemContainerGenerator

Gets the TreeItemContainerGenerator for the tree view.

public TreeItemContainerGenerator ItemContainerGenerator { get; }

Property Value

TreeItemContainerGenerator

SelectedItem

Gets or sets the selected item.

public object? SelectedItem { get; set; }

Property Value

object

Remarks

Note that setting this property only currently works if the item is expanded to be visible. To select non-expanded nodes use Selection.SelectedIndex.

SelectedItems

Gets or sets the selected items.

public IList SelectedItems { get; set; }

Property Value

IList

SelectionMode

Gets or sets the selection mode.

public SelectionMode SelectionMode { get; set; }

Property Value

SelectionMode

Methods

CollapseSubTree(TreeViewItem)

Collapse the specified TreeViewItem all descendent TreeViewItem s.

public void CollapseSubTree(TreeViewItem item)

Parameters

item TreeViewItem

The item to collapse.

ContainerForItemPreparedOverride(Control, object?, int)

Called when a container has been fully prepared to display an item.

protected override void ContainerForItemPreparedOverride(Control container, object? item, int index)

Parameters

container Control

The container control.

item object

The item being displayed.

index int

The index of the item being displayed.

Remarks

This method will be called when a container has been fully prepared and added to the logical and visual trees, but may be called before a layout pass has completed. It is called immediately before the ContainerPrepared event is raised.

CreateContainerForItemOverride(object?, int, object?)

Creates or a container that can be used to display an item.

protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)

Parameters

item object
index int
recycleKey object

Returns

Control

ExpandSubTree(TreeViewItem)

Expands the specified TreeViewItem all descendent TreeViewItems.

public void ExpandSubTree(TreeViewItem item)

Parameters

item TreeViewItem

The item to expand.

GetContainerFromEventSource(object)

Tries to get the container that was the source of an event.

protected TreeViewItem? GetContainerFromEventSource(object eventSource)

Parameters

eventSource object

The control that raised the event.

Returns

TreeViewItem

The container or null if the event did not originate in a container.

GetRealizedTreeContainers()

public IEnumerable<Control> GetRealizedTreeContainers()

Returns

IEnumerable<Control>

NeedsContainerOverride(object?, int, out object?)

Determines whether the specified item can be its own container.

protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)

Parameters

item object

The item to check.

index int

The index of the item.

recycleKey object

When the method returns, contains a key that can be used to locate a previously recycled container of the correct type, or null if the item cannot be recycled. If the item is its own container then by definition it cannot be recycled, so recycleKey shoud be set to null.

Returns

bool

true if the item needs a container; otherwise false if the item can itself be used as a container.

OnCreateAutomationPeer()

Returns a new, type-specific AutomationPeer implementation for the control.

protected override AutomationPeer OnCreateAutomationPeer()

Returns

AutomationPeer

The type-specific AutomationPeer implementation.

OnGotFocus(GotFocusEventArgs)

Called before the GotFocus event occurs.

protected override void OnGotFocus(GotFocusEventArgs e)

Parameters

e GotFocusEventArgs

The event args.

OnKeyDown(KeyEventArgs)

Handles directional navigation within the ItemsControl.

protected override void OnKeyDown(KeyEventArgs e)

Parameters

e KeyEventArgs

The key events.

OnPointerPressed(PointerPressedEventArgs)

Called before the PointerPressed event occurs.

protected override void OnPointerPressed(PointerPressedEventArgs e)

Parameters

e PointerPressedEventArgs

The event args.

SelectAll()

Selects all items in the TreeView.

public void SelectAll()

Remarks

Note that this method only selects nodes currently visible due to their parent nodes being expanded: it does not expand nodes.

TreeContainerFromItem(object)

public Control? TreeContainerFromItem(object item)

Parameters

item object

Returns

Control

TreeItemFromContainer(Control)

public object? TreeItemFromContainer(Control container)

Parameters

container Control

Returns

object

UnselectAll()

Deselects all items in the TreeView.

public void UnselectAll()

UpdateSelectionFromContainer(Control, bool, bool, bool, bool)

Updates the selection for an item based on user interaction.

protected void UpdateSelectionFromContainer(Control container, bool select = true, bool rangeModifier = false, bool toggleModifier = false, bool rightButton = false)

Parameters

container Control

The container.

select bool

Whether the item should be selected or unselected.

rangeModifier bool

Whether the range modifier is enabled (i.e. shift key).

toggleModifier bool

Whether the toggle modifier is enabled (i.e. ctrl key).

rightButton bool

Whether the event is a right-click.

UpdateSelectionFromEventSource(object, bool, bool, bool, bool)

Updates the selection based on an event that may have originated in a container that belongs to the control.

protected bool UpdateSelectionFromEventSource(object eventSource, bool select = true, bool rangeModifier = false, bool toggleModifier = false, bool rightButton = false)

Parameters

eventSource object

The control that raised the event.

select bool

Whether the container should be selected or unselected.

rangeModifier bool

Whether the range modifier is enabled (i.e. shift key).

toggleModifier bool

Whether the toggle modifier is enabled (i.e. ctrl key).

rightButton bool

Whether the event is a right-click.

Returns

bool

True if the event originated from a container that belongs to the control; otherwise false.

Events

SelectionChanged

Occurs when the control's selection changes.

public event EventHandler<SelectionChangedEventArgs>? SelectionChanged

Event Type

EventHandler<SelectionChangedEventArgs>