Component Association

Associate with parent and child component

Parent Component

For .razor file component, you should create cascading component manually

List.razor
@inherits BlazorComponentBase

<CascadingValue Value="this">
    <ul @attributes="AdditionalAttributes">@ChildContent</ul>
</CascadingValue>

For component class, just define ParentComponent attribute

[ParentComponent]
public class List : BlazorComponentBase
{
}

Child Component

Define ChildComponent attribute for child component

ListItem.razor
@inherits BlazorComponentBase
@attribute [ChildComponent(typeof(List))]

<!--For .NET 7 can use generic attribute-->
@attribute [ChildComponent<List>]

<li @attributes="@AdditionalAttributes">
    @ChildContent
</li>

@code{
    [CascadingParameter]public List CascadedList { get; set; }
    
    protected override void BuildCssClass(ICssClassBuilder builder)
    {
        if(CascadedList.Active)
        {
            //..
        }
    }
}

So ListItem component must be inside of List component, or an exception will be thrown if ListItem dose not inside of List component

Optional

Set true ignore the exception if not in parent component

Last updated