Question

How do I find a specific subcomponent?

I have a Blazor server-side app, which simulates a windowing system. This uses the TelerikWindow component. Each child window is a separate component, inheriting from a WindowBase component and containing the code for the specific window.

The component that manages the windows has a collection of WindowParameter objects, which for simplicity can be considered as...

public ObservableCollection<WindowParameters> Windows { get; set; } = [];
public record WindowParameter(Type Type);

When the user opens a window, an entry is added to this collection, and is rendered like this...

@foreach (WindowParameters w in Windows) {
  <DynamicComponent Type="@w.Type" />
}

The real code has much more complexity, but I think this is enough to explain my question.

There are times when the window manager needs to pass some data to a specific window. However, it doesn't have a list of the window components, so can't do that directly. I'm trying to find a way of doing this.

I tried wrapping the foreach loop above in a <CascadingValue>, passing this as the value, but the cascading parameter in the window components was always null.

I thought of looping through the child components of the window manager, looking for components that inherit from WindowBase, but couldn't find a way to do that.

Anyone able to help?

 2  38  2
1 Jan 1970

Solution

 1

Found the answer. Not sure it will help anyone as it was pretty specific, but I'll post it in case.

In order to try and keep the Razor markup clean, and because the rather fab ZenCoding extension doesn't work well with the @if {} else {} statement, we are using a custom component instead. This component takes two RenderFragments and a bool condition, and renders one of the two RenderFragments based on the conditional.

For some reason, the content placed inside these RenderFragments did not get the cascading parameter passed down. By replacing the component with a regular @if {} else {} statement, it all worked fine.

2024-07-24
Avrohom Yisroel