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

    • 9 minutes to read

    TagBox 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

    Use the ItemDisplayTemplate property to customize the appearance of TagBox items. The property accepts a TagBoxItemDisplayTemplateContext<TData> object as the context parameter. You can use the parameter’s members to obtain item information:

    The following code snippet uses the ItemDisplayTemplate property to display TagBox items in a card-like view. Each item shows an employee’s first name, last name, photo, and phone number.

    @inject NwindDataService NwindDataService
    
    <DxTagBox Data="@Data"
              @bind-Values="@Values">
        <TagDisplayTemplate Context="tagContext">
            <div class="tagbox-tag-template">
                <img class="tagbox-tag-template-employee-photo" src="@StaticAssetUtils.GetImagePath(GetImageFileName(tagContext.DataItem))" alt="@tagContext.DataItem.FullName" />
                <div>@tagContext.DataItem.FullName</div>
                <DxButton Click="@tagContext.RemoveTagAction"
                            @onclick:stopPropagation
                            aria-label="Remove Tag"
                            CssClass="tagbox-tag-template-close-btn"
                            IconCssClass="tagbox-tag-template-close-btn-icon"
                    RenderStyle="ButtonRenderStyle.None" RenderStyleMode="ButtonRenderStyleMode.Text">
                </DxButton>
            </div>
        </TagDisplayTemplate>
        <ItemDisplayTemplate>
            <div class="tagbox-item-template">
                <img class="tagbox-item-template-employee-photo" src="@StaticAssetUtils.GetImagePath(GetImageFileName(context.DataItem))" alt="@context.DataItem.FullName" />
                <div>
                    <span class="tagbox-item-template-employee-first-name">@context.DataItem.FullName</span>
                    <span class="tagbox-item-template-employee-home-phone">@context.DataItem.HomePhone</span>
                </div>
            </div>
        </ItemDisplayTemplate>
    </DxTagBox>
    
    @code {
        IEnumerable<Employee> Data { get; set; }
        IEnumerable<Employee> Values { get; set; }
        protected override async Task OnInitializedAsync() {
            Data = await NwindDataService.GetEmployeesAsync();
            Values = Data.Take(1);
        }
        string GetImageFileName(Employee employee) {
            return $"employees/item-template{employee.EmployeeId}.jpg";
        }
    }
    

    TagBox - ItemTemplate

    Run Demo: TagBox - ItemTemplate

    Tag Display Template

    Use the TagDisplayTemplate property to customize tag appearance. The property accepts a TagBoxTagDisplayTemplateContext<TData> object as the context parameter. You can use the parameter’s members to obtain tag information:

    The following code snippet customizes a tag appearance according to its type and text.

    @inject WorldcitiesDataService WorldcitiesDataService
    <DxTagBox Data="@Data"
              TextFieldName="@nameof(City.CityName)"
              TData="City"
              TValue="City"
              AllowCustomTags="true"
              @bind-Tags="@Tags"
              ListRenderMode="ListRenderMode.Virtual">
        <TagDisplayTemplate Context="tagContext">
            @{
                var buttonStyleMode = tagContext.IsCustom ? ButtonRenderStyleMode.Contained : GetModeByID(tagContext.DataItem.CityName);
                var buttonStyle = tagContext.IsCustom ? ButtonRenderStyle.Dark : ButtonRenderStyle.Primary;
                <DxButton RenderStyle="@buttonStyle"
                          RenderStyleMode="@buttonStyleMode"
                          Text="@tagContext.DisplayText"
                          CssStyle="display:inline-block; padding-right: 0">
                    @context
                    <span @onclick="@tagContext.RemoveTagAction" style="display:inline-block; width: 1em; margin-left: 0.5em">
                        &times;
                    </span>
                </DxButton>
            }
        </TagDisplayTemplate>
    </DxTagBox>
    
    @code {
        IEnumerable<City> Data { get; set; }
        IEnumerable<string> Tags { get; set; }
        protected override async Task OnInitializedAsync() {
            Data = await WorldcitiesDataService.GetCitiesAsync();
            Tags = new List<string>() { "New York", "Los Angeles", "Tokyo" };
        }
        ButtonRenderStyleMode GetModeByID(string cityName) {
            switch(cityName) {
                case "New York":
                    return ButtonRenderStyleMode.Contained;
                case "Los Angeles":
                    return ButtonRenderStyleMode.Outline;
                default:
                    return ButtonRenderStyleMode.Text;
            }
        }
    }
    

    TagBox Template

    Run Demo: TagBox - Tag Display Template

    Cell Display Templates

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

    Property Container Scope
    ColumnCellDisplayTemplate DxTagBox All cells in a TagBox
    CellDisplayTemplate DxListEditorColumn Cells in the current column

    These properties can be combined within a single TagBox. 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 TagBox control:

    <DxTagBox Data="Subscriptions.Plans"
                @bind-Values="SelectedPlans"
                EditFormat="{0}">
        <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" />
            <DxListEditorColumn FieldName="PriceQuarter" Caption="Quarter" />
            <DxListEditorColumn FieldName="PriceYear" Caption="Year" />
        </Columns>
        <ColumnCellDisplayTemplate>
            <div style="text-align: right;">
                @($"{context.Value:C}")
            </div>
        </ColumnCellDisplayTemplate>
    </DxTagBox>
    
    @code {
        IEnumerable<SubscriptionPlan> SelectedPlans { get; set; }
    }
    

    TagBox - Cell Display Templates

    Run Demo: TagBox - Cell Display Template

    Empty Data Area Template

    The TagBox displays an empty data area in the following cases:

    • The Data property is unset.
    • The specified data source is empty.
    • None of the TagBox items matches the current search and filter condition.
    • You use the DataAsync property or the CustomData property to bind the TagBox 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 the content displayed in this empty region. The template’s context parameter includes the IsDataLoading property that allows you to determine whether the TagBox is loading data.

    @inject NwindDataService NwindDataService
    
    <DxTagBox Data="@DataItems"
              @bind-Values="@Items"
              CssClass="cw-480"
              NullText="Select an order..."
             >
        <EmptyDataAreaTemplate>
            @if (!context.IsDataLoading) {
                <div class="empty-data-area-template">
                    <div class="d-flex flex-column">
                        No orders found
                    </div>
                </div>
            }
        </EmptyDataAreaTemplate>
    </DxTagBox>
    
    @code {
        IEnumerable<string> DataItems { get; set; }
        IEnumerable<string> Items { get; set; }
    }
    

    TagBox - Empty Data Area Template

    Run Demo: TagBox - Empty Data Area Template