Musaffar Patel
Musaffar Patel

Reputation: 1342

How to use Cascading Type Params in this situation?

Index.razor.cs:

I am developing a grid compoenent which will display a list of items to it:

<Grid Items="Transactions">
    <GridHeader>
        <GridColumn TItem="Transaction">ID</GridColumn>
        <GridColumn TItem="Transaction">Date</GridColumn>
    </GridHeader>
</Grid> 

Then in Grid.razor.cs and GridColumn.razor.cs I use typeparam TItem. But It seems I need to pas TITem as a parameter in every grid column, how can I implement support for something like the below instead:

<Grid Items="Transactions" TItem="Transaction">
    <GridHeader>
        <GridColumn>ID</GridColumn>
        <GridColumn>Date</GridColumn>
    </GridHeader>
</Grid>   

Therefore TItem would would cascade down to all child compoenents (regalrdless of ghow deeply nested they are)

I have a had a look at CascadingTypeParam but information on it is scarce so I'm not sure how to go about using it if it is the solution for the above.

Upvotes: 1

Views: 960

Answers (1)

Brian Parker
Brian Parker

Reputation: 14533

At the top of Grid.razor add:

@attribute [CascadingTypeParameter(nameof(TItem))]

CascadingTypeParameter

Denotes the generic type parameter as cascading. This allows generic type inference to use this type parameter value automatically on descendants that also have a type parameter with the same name.

Upvotes: 7

Related Questions