Skip to main content
All docs
V25.1
  • List Box - Templates

    • 7 minutes to read

    List Box templates are RenderFragment<TValue> type properties. These render fragments can contain both markup and other Razor components. A template’s Razor markup can access an implicit parameter named context. This parameter is derived from the TValue type and contains template-related members.

    You can use the Context attribute to change the parameter name. This name change is essential when you nest components that contain RenderFragment<TValue> properties, otherwise, the following error can occur: The child content element ‘ChildContent’ of component ‘X’ uses the same parameter name (‘context’) ….

    Item Display Template

    The ItemDisplayTemplate property allows you to customize the appearance of List Box items. The property accepts a ListBoxItemDisplayTemplateContext<TData> object as the context parameter. You can use the parameter’s members to get information about items:

    The following code snippet displays List Box items as contact cards. Each item shows an employee’s first name, last name, photo, and phone number.

    @inject NwindDataService NwindDataService
    
    <DxListBox Data="@Data"
                @bind-Value="@Value"
                SizeMode="Params.SizeMode"
                CssClass="cw-400 chi-220">
        <ItemDisplayTemplate>
            <div class="listbox-item-template">
                <img src="@StaticAssetUtils.GetImagePath(GetImageFileName(context.DataItem))" alt="@context.DataItem.FullName" />
                <div class="listbox-item-template-text">
                    <span>@context.DataItem.FullName</span>
                    <span class="listbox-item-template-text-phone">@context.DataItem.HomePhone</span>
                </div>
            </div>
        </ItemDisplayTemplate>
    </DxListBox>
    
    @code {
        IEnumerable<Employee> Data { get; set; }
        Employee Value { get; set; }
    
        protected override async Task OnInitializedAsync() {
            Data = await NwindDataService.GetEmployeesAsync();
            Value = Data.FirstOrDefault();
        }
    
        string GetImageFileName(Employee employee) {
            return $"employees/item-template{employee.EmployeeId}.jpg";
        }
    }
    

    ListBox - Item Display Template

    Run Demo: ListBox - Item Display Template

    Cell Display Templates

    Use the following properties to customize cell appearance in a multi-column List Box.

    Property Container Scope
    ColumnCellDisplayTemplate DxListBox All cells in a List Box
    CellDisplayTemplate DxListEditorColumn Cells in the current column

    These properties can be combined within a single List Box. If both templates are defined, the column-specific CellDisplayTemplate takes precedence over the global ColumnCellDisplayTemplate for cells in that particular column. This functionality allows you to set a common template for all columns and then override it for individual columns that require different formatting.

    The following code snippet customizes the appearance of different columns in a List Box control.

    <DxListBox Data="Subscriptions.Plans"
               @bind-Value="SelectedPlan">
        <Columns>
            <DxListEditorColumn FieldName="Name" Caption="Plan">
                <CellDisplayTemplate>
                    <div style="text-align: left;">
                        <b>@context.Value Subscription</b>
                    </div>
                </CellDisplayTemplate>
            </DxListEditorColumn>
            <DxListEditorColumn FieldName="PriceMonth" Caption="Month" Width="70px" />
            <DxListEditorColumn FieldName="PriceQuarter" Caption="Quarter" Width="70px" />
            <DxListEditorColumn FieldName="PriceYear" Caption="Year" Width="70px" />
        </Columns>
        <ColumnCellDisplayTemplate>
            <div style="text-align: right;">
                @($"{context.Value:C}")
            </div>
        </ColumnCellDisplayTemplate>
    </DxListBox>
    
    @code {
        SubscriptionPlan SelectedPlan { get; set; }
    }
    

    List Box - Cell Display Templates

    Run Demo: List Box - Cell Display Template

    Empty Data Area Template

    The List Box displays an empty data area in the following cases:

    • The Data property is unset.
    • The specified data source does not contain items.
    • You use the DataAsync property or the CustomData property to bind the List Box to a data source. The component sends the first request to a remote data source and waits for a response.

    Use the EmptyDataAreaTemplate to customize content displayed in the empty region. The template’s context parameter includes the IsDataLoading property that allows you to determine whether the List Box data is still loading data.

    The following code snippet embeds the Wait Indicator component into the List Box.

    @inject NwindDataService NwindDataService
    <DxListBox TData="@WebApiLookup" TValue="string"
               ListRenderMode="ListRenderMode.Virtual"
               CssClass="cw-400 chi-220"
               CustomData="@LoadCustomData"
               @key="ComponentKey">
        <EmptyDataAreaTemplate>
            @if (context.IsDataLoading) {
                <div class="empty-data-area-template">
                    <div class="d-flex flex-column">
                        <DxWaitIndicator Visible="true"
                                         CssClass="m-auto"
                                         AnimationType="WaitIndicatorAnimationType.Spin" />
                        <p class="dxbl-text d-block mt-1">Loading, please wait...</p>
                    </div>
                </div>
            }
        </EmptyDataAreaTemplate>
    </DxListBox>
    
    @code {
        [Inject] protected HttpClient Client { get; set; }
        Guid ComponentKey { get; set; } = Guid.NewGuid();
        async Task<LoadResult> LoadCustomData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken) {
            using var response = await Client.GetAsync(options.ConvertToGetRequestUri
                ("https://js.devexpress.com/Demos/NetCore/api/DataGridWebApi/CustomersLookup"), cancellationToken);
            response.EnsureSuccessStatusCode();
            if(options.RequireTotalCount)
                await Task.Delay(3000, cancellationToken);
            using var responseStream = await response.Content.ReadAsStreamAsync();
            var result = await JsonSerializer.DeserializeAsync<LoadResult>(responseStream, cancellationToken: cancellationToken);
            return result;
        }
        void ReloadListBox() {
            ComponentKey = Guid.NewGuid();
        }
        public class WebApiLookup {
            public string Text { get; set; }
            public string Value { get; set; }
        }
    }
    

    ListBox - Empty Data Area Template

    Run Demo: List Box - Empty Data Area Template