Navigation menu

Collapsible side menu

The collapsible side menu plugged into its own Navigator. The collapse button folds it to an icons-only rail. A NavigationGroup displays like an item but expands to reveal children, and groups can nest. A NavigationTitle labels a section and turns into a separator line once collapsed.

When collapsed, each item and group hides its label and surfaces it as a right-placed tooltip on hover, keeping the icons-only rail discoverable. IsCollapsed (bindable) collapses the menu from code. A group’s Icon is what shows on the collapsed rail, and IsExpanded (closed by default) opens it; a group holding the selected item opens on its own.

Under a ThemedWindow with AllowContentOverTitleBar, the menu’s top strip (the Header and the space beside the collapse button) drags the window; the collapse button and the items stay clickable. The Header is therefore not interactive.

<nav:NavigationMenu Navigator="{Binding DemoNavigator}">
    <!-- Optional header slot, hidden when collapsed -->
    <nav:NavigationMenu.Header>
        <StackPanel Orientation="Horizontal" toolkit:Spacing.Gap="8">
            <fonts:FontIcon Text="{x:Static fonts:LucideFontIcons.Rocket}" />
            <TextBlock Text="My app" />
        </StackPanel>
    </nav:NavigationMenu.Header>
    <nav:NavigationTitle>Demo</nav:NavigationTitle>
    <!-- An item targets the type of the page it navigates to -->
    <nav:NavigationItem TargetType="{x:Type vm:HomeViewModel}">
        <nav:NavigationItem.Icon>
            <fonts:FontIcon Text="{x:Static fonts:LucideFontIcons.Home}" />
        </nav:NavigationItem.Icon>
        Home
    </nav:NavigationItem>
    <!-- A group displays like an item but expands to reveal children -->
    <nav:NavigationGroup Header="Parent">
        <nav:NavigationGroup.Icon>
            <fonts:FontIcon Text="{x:Static fonts:LucideFontIcons.Folder}" />
        </nav:NavigationGroup.Icon>
        <nav:NavigationItem TargetType="{x:Type vm:Submenu1ViewModel}">Submenu 1</nav:NavigationItem>
        <nav:NavigationItem TargetType="{x:Type vm:Submenu2ViewModel}">Submenu 2</nav:NavigationItem>
        <!-- Groups can nest -->
        <nav:NavigationGroup Header="Parent">
            <!-- … more items … -->
        </nav:NavigationGroup>
    </nav:NavigationGroup>
</nav:NavigationMenu>

Resolving a target type to a page

An item’s TargetType is the type of the page (view model) it navigates to. The Navigator turns that type into the page instance through the resolver it is built with — usually a lookup in the shell view model’s page registry:

// Pages keyed by their own type, which is what the menu items target.
private readonly Dictionary<Type, object> _pages = new object[]
{
    new HomeViewModel(),
    new Submenu1ViewModel(),
    new Submenu2ViewModel(),
}.ToDictionary(page => page.GetType());

public Navigator Navigator { get; }

public ShellViewModel()
{
    Navigator = new Navigator(target => _pages.GetValueOrDefault(target));
}

An item shows as selected while the current page is of its TargetType, so give each menu entry its own page type. Returning null from the resolver (an unknown type) leaves the current page in place.

Navigating from code takes either form — a type, resolved the same way, or a page instance directly:

Navigator.Navigate(typeof(HomeViewModel));
Navigator.Navigate(new HomeViewModel());

Page lifecycle

A page doesn’t have to implement anything. One that wants to know when it is shown or left implements IPage, whose two methods have empty defaults so only the one needed is written:

public class HomeViewModel : IPage
{
    public void OnNavigatedTo() => Refresh();
}

The Navigator calls OnNavigatedFrom on the page it leaves, then OnNavigatedTo on the new one. Overlay content gets the same calls when it is shown and closed.