Skip to content

Commit aeb8faf

Browse files
csharpfritzCopilot
andcommitted
feat(DetailsView): Add CascadingTypeParameter for cleaner child syntax
DetailsView now cascades its ItemType to child components via [CascadingTypeParameter], matching GridView's behavior. This allows: Before: <BoundField ItemType="Course" DataField="Name" /> After: <BoundField DataField="Name" /> Updated skills documentation to reflect this cleaner pattern. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 923d5dd commit aeb8faf

4 files changed

Lines changed: 24 additions & 16 deletions

File tree

.github/skills/component-development/SKILL.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,25 @@ Data controls that support BoundField, TemplateField, and other column types mus
5050

5151
**How it works:**
5252
1. Parent component (GridView, DetailsView) implements `IColumnCollection<ItemType>`
53-
2. Parent provides cascading value: `<CascadingValue Value="this" Name="ColumnCollection">`
54-
3. Column components (BoundField, TemplateField) have `[CascadingParameter(Name = "ColumnCollection")]`
55-
4. On initialization, columns call `ParentColumnsCollection.AddColumn(this)` to register
53+
2. Parent uses `@attribute [CascadingTypeParameter(nameof(ItemType))]` to cascade the type to children
54+
3. Parent provides cascading value: `<CascadingValue Value="this" Name="ColumnCollection">`
55+
4. Column components (BoundField, TemplateField) have `[CascadingParameter(Name = "ColumnCollection")]`
56+
5. On initialization, columns call `ParentColumnsCollection.AddColumn(this)` to register
57+
6. Child components automatically receive the `ItemType` via Blazor's cascading type parameter — no need to specify `ItemType` on each child
5658

5759
**Example:**
60+
```razor
61+
@* Parent component (.razor file) *@
62+
@typeparam ItemType
63+
@attribute [CascadingTypeParameter(nameof(ItemType))]
64+
65+
<CascadingValue Value="this" Name="ColumnCollection">
66+
@Fields
67+
</CascadingValue>
68+
```
69+
5870
```csharp
59-
// Parent component (DetailsView)
71+
// Parent component (.razor.cs file)
6072
public partial class DetailsView<ItemType> : IColumnCollection<ItemType>
6173
{
6274
private List<IColumn<ItemType>> _columnList = new();
@@ -65,11 +77,6 @@ public partial class DetailsView<ItemType> : IColumnCollection<ItemType>
6577
public void AddColumn(IColumn<ItemType> column) => _columnList.Add(column);
6678
public void RemoveColumn(IColumn<ItemType> column) => _columnList.Remove(column);
6779
}
68-
69-
// In .razor file:
70-
<CascadingValue Value="this" Name="ColumnCollection">
71-
@Fields
72-
</CascadingValue>
7380
```
7481

7582
### Property Naming Convention

.github/skills/webforms-migration/SKILL.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,18 @@ Data controls require additional changes for data binding:
348348
ItemType="Course"
349349
AutoGenerateRows="false">
350350
<Fields>
351-
<BoundField ItemType="Course" DataField="CourseID" HeaderText="Course ID" />
352-
<BoundField ItemType="Course" DataField="CourseName" HeaderText="Course Name" />
353-
<BoundField ItemType="Course" DataField="StudentsMax" HeaderText="Max Students" />
351+
<BoundField DataField="CourseID" HeaderText="Course ID" />
352+
<BoundField DataField="CourseName" HeaderText="Course Name" />
353+
<BoundField DataField="StudentsMax" HeaderText="Max Students" />
354354
</Fields>
355355
</DetailsView>
356356
```
357357

358358
**Key changes:**
359359
- `SelectMethod="GetCourse"``DataItem="_selectedCourse"` (bind to a field loaded in code)
360360
- `ItemType="Namespace.Class"``ItemType="Class"` (use short name, ensure `@using` directive exists)
361-
- BoundField needs `ItemType="Course"` attribute explicitly (required for column registration)
362361
- BoundField, TemplateField, and other column types work inside `<Fields>` just like Web Forms
362+
- Child components (BoundField, TemplateField) **inherit the type parameter** via Blazor's `[CascadingTypeParameter]` — no `ItemType` needed on each child
363363

364364
#### Repeater
365365

samples/AfterContosoUniversity/Pages/Courses.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
CssClass="details"
4545
AutoGenerateRows="false">
4646
<Fields>
47-
<BoundField ItemType="Course" DataField="CourseID" HeaderText="Course ID" />
48-
<BoundField ItemType="Course" DataField="CourseName" HeaderText="Course Name" />
49-
<BoundField ItemType="Course" DataField="StudentsMax" HeaderText="Max Students" />
47+
<BoundField DataField="CourseID" HeaderText="Course ID" />
48+
<BoundField DataField="CourseName" HeaderText="Course Name" />
49+
<BoundField DataField="StudentsMax" HeaderText="Max Students" />
5050
</Fields>
5151
</DetailsView>
5252
}

src/BlazorWebFormsComponents/DetailsView.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@using BlazorWebFormsComponents.DataBinding
22
@inherits DataBoundComponent<ItemType>
33
@typeparam ItemType
4+
@attribute [CascadingTypeParameter(nameof(ItemType))]
45

56
<CascadingValue Name="ParentDetailsView" Value="this" IsFixed="true">
67
@RowStyleContent

0 commit comments

Comments
 (0)