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

    • 5 minutes to read

    This topic describes how to bind the Blazor List Box to data in different scenarios.

    Strongly Typed Collection

    Use the Data property to bind the List Box to a strongly typed collection. Initialize this collection in the OnInitialized lifecycle method or before this method is invoked. Use the DataAsync property instead of the Data property if a strongly typed collection is loaded asynchronously (for instance, from an HTTP request).

    Use the Value or Values property to select one or multiple List Box items programmatically. You can use the @bind attribute to bind these properties to a data field. Refer to the following topic for more information: Two-Way Data Binding.

    <DxListBox Data="@Cities" @bind-Values="@Values"></DxListBox>
    
    @code {
      IEnumerable<string> Cities = new List<string>() {
        "London",
        "Berlin",
        "Paris",
      };
    
      IEnumerable<string> Values { get; set; } 
    }
    

    List Box - Strongly Typed Collection

    The List Box control can detect IQueryable collections and use benefits of the corresponding LINQ query providers (such as Entity Framework).

    Custom Object Collection

    You can bind the List Box to a data collection (Data) that stores custom objects (IEnumerable<CustomType>). The Values collection can also contain custom objects. In this case, you need to override the following methods for your custom object:

    • GetHashCode(). The List Box uses hash codes to compare items. Override this method to make sure that compared objects have the same hash code.

    • Equals. The default Equals method determines whether two object instances are equal. The compared items will not be equal if they are different instances (even if they have exactly the same properties). As a result, the component will not display the selected items.

    You also need to set the List Box’s TextFieldName property. It specifies the custom object’s field name that returns strings to be shown in the List Box. If the TextFieldName property is not specified, the List Box component searches for a Text field in the data source and uses this field as a text field. Otherwise, the List Box is populated with CustomType.ToString() values.

    As an alternative, you can use KeyFieldName and KeyFieldNames properties to specify which field the List Box should use as an identifier to compare items.

    @using StaffData
    
    <DxListBox Data="@Staff.DataSource"
               @bind-Values="@Values"
               TextFieldName="@nameof(Person.Text)">
    </DxListBox>
    
    @code {
        IEnumerable<Person> Values { get; set; } = Staff.DataSource.Take(1);
    }
    

    List Box

    Run Demo: List Box - Overview

    Load Custom Data

    When you bind an editor to data from a Web API service, assign the data type to the component’s T parameter and use the CustomData property to implement custom data load logic.

    View Example: Data Editors for Blazor - Custom data binding

    You can also enable Virtual Scrolling to improve client-side performance.

    Virtual Scrolling

    When virtual scrolling is activated (ListRenderMode is set to Virtual), the List Box renders data on demand when a user scrolls its items.

    @using StaffData
    
    <DxListBox Data="@Staff.DataSource"
               TData="Person"
               TValue="Person"
               ListRenderMode="ListRenderMode.Virtual">
    </DxListBox>
    

    Run Demo: List Box - Virtual Scrolling