Tree Page View Model
Overview
TreePageViewModel<TContext,TSubPage> is an abstract class that powers tree‑structured pages in Asv.Avalonia. It builds on top of PageViewModel and adds:
A hierarchical menu tree (
TreeView) that can be navigated.Management of selected nodes and the corresponding subpage (
SelectedPage).Breadcrumb navigation (
BreadCrumb).Commands to show/hide the side menu.
Proper disposal of dynamically created resources.
Typical use cases include settings panels or any UI where a left‑hand tree selects a detail view on the right.
The subpages displayed on the right are implemented using TreeSubpage.
Core Components
Navigation Flow
User selects a node →
SelectedNodechanges.SelectedNodeChangedskips null and internal navigations, rebuilds the breadcrumb from the node's path to the root, and callsNavigate(node.Base.NavigateTo).Navigateeither creates a new subpage through the DI container (CreateSubPage) or builds a default page (CreateDefaultPage).The new subpage becomes the
SelectedPage; the old subpage (if any) is disposed.
Tree Structure
Nodes is a flat collection of ITreePageMenuItem: root items and nested items alike are added straight into it. The hierarchy is not expressed by nesting — TreeView derives it from each item's Id and ParentId, treating NavId.Empty as the root.
Breadcrumb Navigation
The breadcrumb automatically updates based on the selected node, showing the path from the root to the current selection.
Menu Visibility
You can show or hide the side menu using ShowMenuCommand and HideMenuCommand. The IsMenuVisible property reflects the current state.
Layout Management
The tree page automatically saves and restores its state through the Layout controller. In AfterLoadExtensions it registers two layout values:
the selected node, stored as the node key string under
nameof(SelectedNode);IsMenuVisible, stored undernameof(IsMenuVisible).
Both are saved whenever they change, and loaded once the page is attached to the shell — so when the page is reopened, it restores the previously selected node and the menu state.
Default Page
If no specific subpage is found for a navigation ID, the CreateDefaultPage() method is called. By default, it creates a GroupTreePageItemViewModel which displays all child nodes of the selected tree node as clickable cards.
Example
A typical usage example is a Settings page. It consists of a tree structure where nodes correspond to subpages with concrete settings (e.g., shortcuts, units, etc.).
First, create an ISettingsPage interface for the settings page. ITreePageViewModel already supplies the Nodes collection, so the interface only has to name the page:
Next, create the SettingsPageViewModel:
To add nodes to the tree view, use extensions. The DefaultSettingsExtension resolves all ITreePageMenuItem services keyed by the parent page ID and adds them to the tree:
Tree menu nodes are registered as keyed services in the DI container (keyed by the parent page ID), so the extension automatically picks them up.
Registration
All components must be registered in the builder chain. A typical registration for a tree page:
A subpage needs its view model, its view, and its tree menu node. The first two are registered together by the TreePage builder, which wires up the keyed view model (with its tree subpage context) and the view locator entry:
In practice, the registration builder SettingsPageRegistrations.Builder wraps both calls into a single one:
For information on how to create subpages that are displayed when tree nodes are selected, see TreeSubpage.
API
ITreePageViewModel
Property | Type | Description |
|---|---|---|
|
| Gets the flat collection of all menu items. The hierarchy is built from each item's |
TreePageViewModel<TContext, TSubPage>: ITreePageViewModel
Represents a page with a tree-based navigation structure. Extends PageViewModel<TContext> and manages a tree menu with corresponding subpages.
TreePageViewModel constructor
Constructor | Description |
|---|---|
| Protected. Creates the page along with its |
Property | Type | Description |
|---|---|---|
|
| Gets or sets the icon shown in the tree menu header. The default view falls back to the page's |
|
| Gets or sets the text shown in the tree menu header. The default view falls back to the page's |
|
| Command to show the side menu. |
|
| Command to hide the side menu. |
|
| Indicates whether the side menu is currently visible. |
|
| Gets the tree structure for navigation. |
|
| Gets or sets the currently displayed subpage. |
|
| Gets the breadcrumb navigation items. |
|
| Gets or sets the currently selected tree node. |
|
| Gets the flat collection from which |
|
| Protected. The context object extensions are applied to (typically the page itself). |
Method | Return Type | Description |
|---|---|---|
|
| Navigates to the subpage with the specified navigation ID. |
|
| Returns every item from |
|
| Creates a default subpage when no specific subpage is found. |
|
| Creates a new subpage instance through the DI container by ID. |
|
| Called after all extensions have been loaded. Registers the page's layout values. |