liveBook · Manning

ChildContent expression

As explained in Pro ASP.NET Core 7, Tenth Edition

ChildContent is the Blazor component parameter that receives and renders content supplied between a component's opening and closing tags.

ChildContent lets a Blazor component act as a wrapper around markup supplied between its opening and closing tags. The component declares a [Parameter] property of type RenderFragment, then renders that content with @ChildContent. section 34.3 section 34.5

Use the feature by following this pattern:

  • Declare a nullable RenderFragment? property named ChildContent and decorate it with [Parameter]. section 34.3
  • Place @ChildContent where the supplied markup should appear in the component’s output. section 34.3
  • Apply the component with ordinary opening and closing tags; Blazor assigns the enclosed markup to ChildContent automatically. section 34.3

ThemeWrapper

A ThemeWrapper receives Theme and Title parameters, then places @ChildContent inside a styled Bootstrap div. Its caller can wrap a SelectFilter and button without adding configuration for the child markup. ErrorBoundary uses the same tag-based idea with ChildContent for normal output and ErrorContent for failures.

Listing 34.15 The contents of the ThemeWrapper.razor file in the Blazor folder
<div class="p-2 bg-@Theme border text-white">
   <h5 class="text-center">@Title</h5>
   @ChildContent
</div>

@code {
   [Parameter]
   public string? Theme { get; set; }

   [Parameter]
   public string? Title { get; set; }

   [Parameter]
   public RenderFragment? ChildContent { get; set; }
}

Inspect the complete ThemeWrapper implementation, including its styling parameters, RenderFragment property, and @ChildContent expression. section 34.3

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Defines the RenderFragment parameter, rendering expression, tag syntax, and automatic assignment, but does not detail RenderFragment internals.
Examples
Uses ThemeWrapper around filters and buttons, and ErrorBoundary around selectable content with separate normal and error markup.
What it assumes
Assumes basic Blazor component markup and parameters while introducing RenderFragment-based child content from the implementation pattern.
What you can do after
Build wrapper components that accept arbitrary markup between their tags and place it at a chosen location in the output.
How it connects
Connects Blazor components with RenderFragment, Parameter attributes, wrapper components, ErrorBoundary, and ErrorContent.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.