I'm hoping someone can help as I'm new to Blazor.
I've started a new Blazor project and am trying to add contextual buttons in the top bar that can be changed by / based on the child component (when you navigate to page 1 it can show different things to when you navigate to page 2)
This is my approach so far:
TopBar.razor - A template for the top bar with render fragment so I can render different stuff
@inject Learning.Data.TopBarContext TopBarContext
<div class="col-sm-6">@Context</div>
<span class="col-sm-2">@Text1</span>
<span class="col-sm-2">@Text2</span>
@code {
[Parameter]
private RenderFragment Context { get; set; }
[Parameter]
public string Text1 { get; set; } = "Not Set";
[Parameter]
public string Text2 { get; set; } = "Not Set";
}
MainLayout.razor - I use TopBar and cascade it down to all the pages
@inherits LayoutComponentBase
<div class="sidebar">
<NavMenu />
</div>
<div class="main">
<div class="top-row px-4 auth">
<TopBar />
</div>
<div class="content px-4">
<CascadingValue Value="TopBar" Name="TopBar">
@Body
</CascadingValue>
</div>
</div>
@code {
private TopBar TopBar;
}
Now I have to two page components:
Page1.razor - When navigated to will render 3 buttons in top bar
@page "/page1"
<button>One</button>
<button>Two</button>
<button>Three</button>
@code {
}
Page2.razor - When navigated to will render 2 buttons in top bar
@page "/page2"
<button>One</button>
<button>Two</button>
@code {
}
I know I can use the code below to pick up the cascaded value:
[CascadingParameter(Name = "TopBar")]
protected string TopBar { get; set; }
But not sure how to proceed, I was hoping for something like cascading the whole component down so that in page1 I could do something flexible like:
<TopBar>
<button>One</button>
<button>Two</button>
<button>Three</button>
</TopBar>
I may be going about this completely the wrong way (I did think creating a registered singleton might be a way to do it) but any clarification you could offer on how to accomplish this would be greatly appreciated!
Many Thanks
Nick
