Table of Contents

Class StdTreeView

Namespace
Alternet.UI
Assembly
Alternet.UI.Common.dll

Displays a hierarchical collection of labeled items with optional images, each represented by a TreeViewItem. This control uses internal VirtualListControl for displaying items, it can be accessed via ListBox property.

[ControlCategory("Common")]
public class StdTreeView : Border, IBaseObjectWithAttr, IBaseObjectWithId, INotifyPropertyChanged, IDisposableObject, IBaseObject, ISupportInitialize, IFocusable, ITextProperty, IComponent, IControl, IDisposable, IWin32Window, ILayoutItem, INotifyDataErrorInfo, ITreeViewItemContainer
Inheritance
StdTreeView
Implements
Derived

Examples

Here is how to declare a TreeView in UIXML:

<StdTreeView SuggestedWidth="300" SuggestedHeight="240" Name="treeView"
          SelectionChanged="TreeView_SelectionChanged"
          ExpandedChanged="TreeView_ExpandedChanged" />

And the corresponding event handler in the code-behind:

private void TreeView_SelectionChanged(object? sender, EventArgs e)
{
    App.Log($"StdTreeView: SelectionChanged. SelectedItem: {treeView.SelectedItem?.Text}");
}

private void TreeView_ExpandedChanged(object? sender, TreeViewEventArgs e)
{
    App.Log($"StdTreeView: ExpandedChanged. Item: {e.Item.Text} {e.Item.IsExpanded}");
}

Also, a TreeView can be created from code:

static void AddItems(StdTreeView treeView, int count)
{
    var r = new Random();

    int start = treeView.Items.Count + 1;

    treeView.BeginUpdate();
    try
    {
        for (int i = start; i < start + count; i++)
        {
            int imageIndex = r.Next(4);
            var item = new TreeViewItem("Item " + i)
            {
                ImageIndex = imageIndex,
            };
            for (int j = 0; j < 3; j++)
                item.Add(new TreeViewItem(item.Text + "." + j, r.Next(4)));
            treeView.Add(item);
        }
    }
    finally
    {
        treeView.EndUpdate();
    }
}

public Alternet.UI.StdTreeView CreateTreeView()
{
    var result = new Alternet.UI.StdTreeView();
    AddItems(result, 10);
    return result;
}

Constructors

StdTreeView()

Initializes a new instance of the StdTreeView class.

Fields

DefaultLevelMargin

Default margin for each level in the tree.

DefaultTreeButtons

Specifies the default type of buttons used in the tree view control to expand or collapse nodes.

ItemPropertyNamesToRaiseInvalidate

Gets or sets the names of properties of TreeViewItem that should raise an invalidation of the container when changed.

Properties

AllowLabelEdit

Gets a value indicating whether the label text of the tree items can be edited. Default is false. Currently, setter of this property does nothing.

BackgroundColor

Gets or sets the background color for the control.

ContextMenu

Alias for ContextMenuStrip property.

ContextMenuStrip

Gets or sets the ContextMenuStrip associated with this control. This property is auto-created and is always not null. Usage of this property depends on the control.

FirstItem

Gets first root item in the control or null if there are no items.

ForegroundColor

Gets or sets the foreground color for the control.

FullRowSelect

Gets a value indicating whether the selection highlight spans the width of the tree control. Default is true. Currently, setter of this property does nothing.

HasOwnInterior

Gets a value indicating whether the control has internal scroll bars.

Header

Gets the header with columns associated with the control.

ImageList

Gets or sets the ImageList associated with the tree control.

Items

Gets the collection of root items contained within the tree control.

LastItem

Gets last item in the control or null if there are no items.

LastRootItem

Gets last root item in the control or null if there are no items.

ListBox

Gets the underlying VirtualListBox used by this tree control.

RealBackgroundColor

Gets real background color for the control.

RealForegroundColor

Gets real foreground color for the control.

RootItem

Gets or sets the root item of the tree view.

RowLines

Gets a value indicating whether to draw a contrasting border between displayed rows. Default is false. Currently, setter of this property does nothing.

SelectedItem

Gets or sets the selected item in the tree view.

SelectedItems

Gets a collection containing the currently selected items in the control. This is slower than SelectedListItems.

SelectedListItems

Gets a collection containing the currently selected items in the control.

SelectionMode

Gets or sets the selection mode (single or multiple).

ShowExpandButtons

Gets or sets a value indicating whether expand buttons are displayed next to tree items that contain child tree items.

ShowLines

Gets a value indicating whether lines are drawn between tree items in the tree view control. Default is false. Currently, setter of this property does nothing.

ShowRootLines

Gets a value indicating whether lines are drawn between the tree items that are at the root of the tree view. Default is false. Currently, setter of this property does nothing.

TopIndex

Gets or sets the index of the first visible item in the control. Returned value is the index in VisibleItems collection.

TopItem

Gets or sets the first fully-visible tree item in the control. This is slower than TopIndex, so if you have item index, use that instead.

TrackItemPropertyChanges

Gets or sets a value indicating whether to track changes of TreeViewItem properties. Default is true.

TrackItemSelectedChanges

Gets or sets a value indicating whether to track changes of TreeViewItem selection state. Default is true.

TreeButtons

Gets or sets the type of tree view buttons.

TwistButtons

Gets or sets a value indicating whether to select alternative style of +/- buttons and to show rotating ("twisting") arrows instead.

VisibleItems

Gets the collection of visible items contained in the tree view.

Methods

Add(TreeViewItem, bool)

Adds the specified item to the tree view on the root level.

Add(string)

Adds a new item with the specified title to the tree view on the root level.

AddChild(TreeViewItem?, TreeViewItem, bool)

Adds a child item to the specified parent item in the tree view.

AddSeparator()

Adds a separator item to the tree control.

ApplyVisibility(bool, EnumExpandedItemsParams?)

Applies a visibility to all items in the tree control, updating their IsVisible property.

ApplyVisibilityFilter(Predicate<TreeViewItem>, EnumExpandedItemsParams?)

Applies a visibility filter to items within the tree control, updating their IsVisible property based on a predicate condition.

ApplyVisibilityFilter(string?, StringComparison, EnumExpandedItemsParams?)

Applies a visibility filter to items within the tree control, updating their IsVisible property based on a text-matching condition.

BeginUpdate()

Maintains performance while performing slow operations on a control by preventing the control from drawing until the EndUpdate() method is called.

Clear()

Clears all items from the tree view.

ClearSelected()

Unselects all items in the control.

CollapseAll()

Collapses all child tree items.

CreateHeader()

Creates and returns a new instance of a ListBoxHeader.

CreateListBox()

Creates a new instance of the VirtualListBox used by the tree control.

EndUpdate()

Resumes painting the control after painting is suspended by the BeginUpdate() method.

EnsureVisible(TreeViewItem?)

Ensures that the tree item is visible, expanding tree items and scrolling the tree view control as necessary.

EnsureVisible(int)

Ensures that the child item of the root item with the specified index is visible within the tree view control.

ExpandAll()

Expands all child tree items.

ExpandAllParents(TreeViewItem?)

Expands all parent items of the specified item.

GetDefaultParentBackColor()

Gets default value for ParentBackColor property.

GetDefaultParentForeColor()

Gets default value for ParentForeColor property.

GetLevelMargin()

Gets margin for each level in the tree.

GetNodeAt(PointD)

Retrieves the tree view item that is at the specified point.

GetNodeAtMouseCursor()

Retrieves the tree control item located at the current mouse cursor position.

InvalidateWhenItemPropertyChanged(TreeViewItem, string?)

Determines whether the tree view should be invalidated when a property of an item changes.

ItemContainsText(TreeViewItem?, string?, StringComparison)

Gets a value indicating whether the specified item contains the specified substring.

MakeAsListBox()

Changes visual style of the control to look like ListBox.

OnContextMenuCreated(EventArgs)

Called when the ContextMenuCreated event is raised.

OnItemPropertyChanged(TreeViewItem, string?)

Called when a property of a tree item changes.

OnItemSelectedChanged(TreeViewItem, bool)

Called when the selection state of a tree item changes.

OnListBoxAsteriskKeyDown(KeyEventArgs)

Handles the behavior when the asterisk (*) key is pressed in the inner list box.

OnListBoxDoubleClick(object?, EventArgs)

Handles the double-click event on the inner list box, toggling the expanded state of the selected item while preserving its position and selection.

OnListBoxKeyDown(object?, KeyEventArgs)

Handles the KeyDown event for the inner list box.

OnListBoxLeftKeyDown(KeyEventArgs)

Handles the behavior when the left arrow key in the inner list box.

OnListBoxMouseDown(object?, MouseEventArgs)

Handles the MouseDown event for the inner ListBox.

OnListBoxRightKeyDown(KeyEventArgs)

Handles the right arrow key press event for the inner list box, expanding the selected item if it has child items.

OnSystemColorsChanged(EventArgs)

Called when the SystemColorsChanged event is raised.

RaiseAfterCollapse(TreeViewItem)

Called after the tree item is collapsed.

RaiseAfterExpand(TreeViewItem)

Called after the tree item is expanded.

RaiseBeforeCollapse(TreeViewItem, ref bool)

Called before the tree item is collapsed.

RaiseBeforeExpand(TreeViewItem, ref bool)

Called before the tree item is expanded.

RaiseExpandedChanged(TreeViewItem)

Called after 'IsExpanded' property value of a tree item belonging to this tree view changes.

RaiseItemAdded(TreeViewItem)

Called when an item is added to this tree view control, at any nesting level.

RaiseItemRemoved(TreeViewItem)

Called when an item is removed from this tree view control, at any nesting level.

RefreshTree()

Refreshes the tree view by updating the list of visible items based on the current state of the tree structure.

Remove(TreeViewItem?)

Removes the specified item (with sub-items) from the tree view.

RemoveAll()

Removes all items from the tree view control.

RemoveItemAndSelectSibling(TreeViewItem?)

Removes item and selects its sibling (next or previous on the same level).

RemoveSelected()

Removes selected items from the control.

RemoveSelectedItem()

Removes the currently selected item from the tree view.

RemoveSelectedItem(bool)

Removes the currently selected item from the tree view and optionally selects on of the remaining items.

ResetCachedImagesInItems()

Resets the cached images for all items in the collection.

ScrollIntoView(TreeViewItem?)

Scrolls control so the specified item will be fully visible.

SelectFirstItem()

Selects the first item in the tree view control.

SelectFirstItemAndScroll()

Selects the first item in the tree view control and scrolls to it.

SelectItem(TreeViewItem?)

Selects the specified item in the tree view and scrolls to it.

SelectLastItem()

Selects the last item in the tree view control.

SelectLastItemAndScroll()

Selects the last item in the tree view control and scrolls to it.

SelectNextTreeButton()

Selects the next type of tree view button.

SetExpandedAndKeepPosAndSelection(TreeViewItem?, bool)

Expands or collapses the specified TreeViewItem while preserving the current scroll position and selection.

SetFocus()

Sets input focus to the control.

SetSelected(TreeViewItem, bool)

Selects or clears the selection for the specified item.

ToggleExpanded(TreeViewItem?)

Toggles the expanded or collapsed state of the specified tree control item.

ToggleExpandedAndCollapseSiblings(TreeViewItem?, bool)

Toggles the expanded state of the specified tree item and optionally collapses its sibling items. If collapseSiblings is true and the item has a parent, all sibling items are collapsed before toggling.

ToggleExpandedAndKeepPosAndSelection(TreeViewItem?)

Toggles the expanded state of the specified TreeViewItem while preserving the current scroll position and selection.

TreeChanged()

Updates the tree view when the tree structure changes.

Events

AfterCollapse

Occurs after the tree item is collapsed.

AfterExpand

Occurs after the tree item is expanded.

BeforeCollapse

Occurs before the tree item is collapsed. This event can be canceled.

BeforeExpand

Occurs before the tree item is expanded. This event can be canceled.

ExpandedChanged

Occurs after 'IsExpanded' property value of a tree item belonging to this tree view changes.

ItemAdded

Occurs when an item is added to this tree view control, at any nesting level.

ItemPropertyChanged

Occurs when a property of a TreeViewItem changes. This is not fired for all properties of the item.

ItemRemoved

Occurs when an item is removed from this tree view control, at any nesting level.

ItemSelectedChanged

Occurs when the selection state of an item changes.

SelectionChanged

Occurs when the selection has changed.