Library Blazor Components TreeView Reference Classes TreeView ItemTemplate

ItemTemplate

Specifies template used for rendering each item. If not set, an default template will be used.

razor
[Parameter]
public RenderFragment<TreeStateContext<T>> ItemTemplate { get; set; }

When building the template, instance of TreeStateContext is available. This class provides properties such as IsSelected, IsFocused and also Level property.

Example

Using this example different rendering can be set if item is a root level item, or not. Also different content can be rendered depending on item selected state.

razor
<TreeView T="Child" ItemsSource="Tree">
    <ItemTemplate>
        @if (context.Level == 0)
        {
            // Top level items
        }
        else
        {
            // Non top-level items          
        }
        <span>
            @context.Value.Title
        </span>
        @if (context.IsSelected)
        {
            // Selected code
        }
    </ItemTemplate>
</TreeView>

Sign in