I'm playing around with the custom template in Blazor and I'm trying to find to a way to two-way bind a CascadingValue
or achieve something similar. Right now I have the following template.
@if (PopupVisible)
{
<DxPopup>
<HeaderTemplate>
<h4 class="modal-title">@HeaderText</h4>
<button type="button" class="close" @onclick="@UpdatePopupVisible">×</button>
</HeaderTemplate>
<ChildContent>
<div class="modal-body">
<div class="container-fluid">
@bodyContent
</div>
</div>
<div class="modal-footer">
@footerContent
<button class="btn btn-secondary" @onclick="UpdatePopupVisible">Cancel</button>
</div>
</ChildContent>
</DxPopup>
}
@code {
[CascadingParameter] public bool PopupVisible { get; set; }
[CascadingParameter] public EventCallback<bool> PopupVisibleChanged { get; set; }
[Parameter] public RenderFragment HeaderText { get; set; }
[Parameter] public RenderFragment footerContent { get; set; }
[Parameter] public RenderFragment bodyContent { get; set; }
private async Task UpdatePopupVisible()
{
PopupVisible = false;
await PopupVisibleChanged.InvokeAsync(PopupVisible);
}
}
Then I have a component that implements this template(child), and then I have that component called with a button press(parent). What I want to know is if there is a way to bind the PopupVisible
parameter from the parent without having to bind it the child and having the child pass it to the template. I haven't found a way to two-way bind a cascading parameter but if possible I think that would be the best way to do so. Outside of that, I'm not sure if there is another way or I'm going to have to go with my current idea of passing the value.
To use two-way binding on a parameter simply prefix the HTML attribute with the text @bind- . This tells Blazor it should not only push changes to the component, but should also observe the component for any changes and update its own state accordingly.
Blazor also supports two-way data binding by using bind attribute. Currently, Blazor supports only the following data types for two-way data binding. If you need other types (e.g. decimal), you need to provide getter and setter from/to a supported type.
Blazor comes with a special component called CascadingValue . This component allows whatever value is passed to it to be cascaded down its component tree to all of its descendants.
Data binding is the connection bridge between View and the business logic (View Model) of the application. The following are the ways of doing data banding with Blazor. One-way data binding is also known as an interpolation in other frameworks, such as Angular.
You can't do two-way binding with cascading parameters. Cascading means flowing downstream, from parent to child, and not the other way around.
I'm not sure I understand your question...however, if you wish to pass a value from a parent component and back; you can do the following: Note: This is a two-way Component data binding
@code
{
private bool visible;
[Parameter]
public bool PopupVisible
{
get { return visible }
set
{
if (visible != value)
{
visible = value;
}
}
}
[Parameter] public EventCallback<bool> PopupVisibleChanged { get; set; }
// Invoke the EventCallback to update the parent component' private field visible with the new value.
private Task UpdatePopupVisible()
{
PopupVisible = false;
return PopupVisibleChanged.InvokeAsync(PopupVisible);
}
}
@page "/"
<DxPopup @bind-PopupVisible="visible" />
@code {
private bool visible;
}
Note: If you need some explanation, and believe that I did not answer your question, don't hesitate to tell me, but please take the time to phrase your questions... I could not completely understand questions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With