Q:

How to define context in a template component in Blazor?

0

This code will help you to understand how to define the context in a template component in Blazor.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

A parameter can be accessed by the template component (of type RenderFragment<T>) using the context property. The context parameter is the implicit parameter; however, the parameter can be changed using the Context attribute on the templated component.

<TableTemplate Items="@forecasts">
    <TableHeader>
        <th>Date</th>
        <th>Temp. (C)</th>
        <th>Temp. (F)</th>
        <th>Summary</th>
    </TableHeader>
    <RowTemplate Context="forecast">
        <td>@forecast.Date.ToShortDateString()</td>
        <td>@forecast.TemperatureC</td>
        <td>@forecast.TemperatureF</td>
        <td>@forecast.Summary</td>
    </RowTemplate>
</TableTemplate>

Alternatively, this attribute can be specified on the component element. So, this attribute applies to all specified template parameters.

<TableTemplate Items="@forecasts" Context="forecast">
    <TableHeader>
        <th>Date</th>
        <th>Temp. (C)</th>
        <th>Temp. (F)</th>
        <th>Summary</th>
    </TableHeader>
    <RowTemplate>
        <td>@forecast.Date.ToShortDateString()</td>
        <td>@forecast.TemperatureC</td>
        <td>@forecast.TemperatureF</td>
        <td>@forecast.Summary</td>
    </RowTemplate>
</TableTemplate>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now