Search code examples
blazorblazor-server-side

How can I get a CascadingParameter define in a library


In the code for the appointment save button it has (I legitimately have the source code):

[CascadingParameter] 
protected internal ISchedulerViewOwner SchedulerViewOwner { get; private set; }

I tried declaring that in my component, that uses the DxScheduler component and a lot of the associated components (although not this component).

It is null.

What can I do to get the cascading parameter? Shouldn't it be available to any component?

For the curious, I am asking because I need to call SchedulerViewOwner.ValidateEditForm


Solution

  • A cascaded parameter is available to any component in the rendertree below the component that cascades it. It has to be in the ChildContent of the cascade. It's always public to any sub components no matter how you declare it in the parent.

    In your example, if DxScheduler cascades the value then <DxScheduler> you can capture it here </DxScheduler>.

    <DxScheduler>
      <AppointmentFormLayout Context="formInfo">
        <DxFormLayoutTabPages>
          <DxFormLayoutTabPage Caption="Event">
    
            <MyCaptureComponent @ref="_myCaptureComponent">
    
            <!-- lots of stuff -->
          </DxFormLayoutTabPage>
        </DxFormLayoutTabPages>
      </AppointmentFormLayout>
    </DxScheduler>
    

    In theory you can add a component within DxScheduler, get a reference to it in your form component and then interact with the captured parameter. You may need to experiment where in the structure it works best.

    However DxScheduler will not be a standard component. It will be like QuickGrid. The ChildContent may only be used to register content objects. Your component may only exist for the registration process.

    Your problem is you are trying to do something with someone else's component for which it wasn't designed. Future updates to that component won't take your hack into account and may break your code.