You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/components/templated-components.md
+45-2Lines changed: 45 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,11 +18,13 @@ Templated components are components that accept one or more UI templates as para
18
18
* A table component that allows a user to specify templates for the table's header, rows, and footer.
19
19
* A list component that allows a user to specify a template for rendering items in a list.
20
20
21
+
[View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/blazor/common/samples/) ([how to download](xref:index#how-to-download-a-sample))
22
+
21
23
## Template parameters
22
24
23
25
A templated component is defined by specifying one or more component parameters of type <xref:Microsoft.AspNetCore.Components.RenderFragment> or <xref:Microsoft.AspNetCore.Components.RenderFragment%601>. A render fragment represents a segment of UI to render. <xref:Microsoft.AspNetCore.Components.RenderFragment%601> takes a type parameter that can be specified when the render fragment is invoked.
@@ -39,6 +41,21 @@ When using a templated component, the template parameters can be specified using
39
41
<td>@context.Name</td>
40
42
</RowTemplate>
41
43
</TableTemplate>
44
+
45
+
@code {
46
+
private List<Pet> pets = new List<Pet>
47
+
{
48
+
new Pet { PetId = 2, Name = "Mr. Bigglesworth" },
49
+
new Pet { PetId = 4, Name = "Salem Saberhagen" },
50
+
new Pet { PetId = 7, Name = "K-9" }
51
+
};
52
+
53
+
private class Pet
54
+
{
55
+
public int PetId { get; set; }
56
+
public string Name { get; set; }
57
+
}
58
+
}
42
59
```
43
60
44
61
> [!NOTE]
@@ -59,6 +76,10 @@ Component arguments of type <xref:Microsoft.AspNetCore.Components.RenderFragment
59
76
<td>@pet.Name</td>
60
77
</RowTemplate>
61
78
</TableTemplate>
79
+
80
+
@code {
81
+
...
82
+
}
62
83
```
63
84
64
85
Alternatively, you can specify the `Context` attribute on the component element. The specified `Context` attribute applies to all specified template parameters. This can be useful when you want to specify the content parameter name for implicit child content (without any wrapping child element). In the following example, the `Context` attribute appears on the `TableTemplate` element and applies to all template parameters:
@@ -74,11 +95,15 @@ Alternatively, you can specify the `Context` attribute on the component element.
74
95
<td>@pet.Name</td>
75
96
</RowTemplate>
76
97
</TableTemplate>
98
+
99
+
@code {
100
+
...
101
+
}
77
102
```
78
103
79
104
## Generic-typed components
80
105
81
-
Templated components are often generically typed. For example, a generic `ListViewTemplate` component can be used to render `IEnumerable<T>` values. To define a generic component, use the [`@typeparam`](xref:mvc/views/razor#typeparam) directive to specify type parameters:
106
+
Templated components are often generically typed. For example, a generic `ListViewTemplate` component (`ListViewTemplate.razor`) can be used to render `IEnumerable<T>` values. To define a generic component, use the [`@typeparam`](xref:mvc/views/razor#typeparam) directive to specify type parameters:
@@ -90,6 +115,20 @@ When using generic-typed components, the type parameter is inferred if possible:
90
115
<li>@pet.Name</li>
91
116
</ItemTemplate>
92
117
</ListViewTemplate>
118
+
119
+
@code {
120
+
private List<Pet> pets = new List<Pet>
121
+
{
122
+
new Pet { Name = "Mr. Bigglesworth" },
123
+
new Pet { Name = "Salem Saberhagen" },
124
+
new Pet { Name = "K-9" }
125
+
};
126
+
127
+
private class Pet
128
+
{
129
+
public string Name { get; set; }
130
+
}
131
+
}
93
132
```
94
133
95
134
Otherwise, the type parameter must be explicitly specified using an attribute that matches the name of the type parameter. In the following example, `TItem="Pet"` specifies the type:
@@ -100,4 +139,8 @@ Otherwise, the type parameter must be explicitly specified using an attribute th
0 commit comments