I'm wondering if it's possible to reference child elements in the ChildContent parameter of a component. I can pass values from parent components to children explicitly or by using a cascading parameter, but there is no great way for a parent component to "know" about elements inside the ChildContent RenderFragment. A possible use-case for this would be a UIList component that gets the number of UIListItems that have been added to the UIList component during the render process.
I've attempted to override the BuildRenderTree method on my component to manually manipulate ChildContent RenderFragment but my only options are to render or ignore the fragment (I can't peek the content of the RenderFragment delegate).
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
int seq = 0;
base.BuildRenderTree(builder);
builder.OpenElement(seq++, "div");
builder.AddContent(seq++, this.ChildContent);
builder.CloseElement();
}
I'm wondering if something like below is possible. Please note that the syntax below is not valid, only an example to get the point across.
protected int NumberOfItems{get;set;}
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
int seq = 0;
base.BuildRenderTree(builder);
builder.OpenElement(seq++, "div");
foreach(var childFragment in this.ChildContent.GetFragments())
{
if(childFragment is UIListItem)
NumberOfItems++;
builder.AddContent(seq++,childFragment);
}
builder.CloseElement();
}