简体   繁体   English

如何在 Blazor 中以两种方式绑定级联值?

[英]How to two way bind a cascading value in Blazor?

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.我正在玩 Blazor 中的自定义模板,我试图找到一种方法来双向绑定CascadingValue或实现类似的东西。 Right now I have the following template.现在我有以下模板。

@if (PopupVisible)
{
    <DxPopup>
        <HeaderTemplate>
            <h4 class="modal-title">@HeaderText</h4>
            <button type="button" class="close" @onclick="@UpdatePopupVisible">&times;</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.我想知道的是,是否有一种方法可以从父级绑定PopupVisible参数,而不必将其绑定到子级并让子级将其传递给模板。 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.除此之外,我不确定是否还有其他方法,或者我将不得不采用我目前传递值的想法。

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您可以执行以下操作: 注意:这是一个双向的组件数据绑定

Child Component子组件

@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);
    }
}

Usage用法

@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.注意:如果您需要一些解释,并认为我没有回答您的问题,请不要犹豫,告诉我,但请花时间说出您的问题......我无法完全理解问题。

what you can do is, Cascade the parent component and in the child component, access the parent Property you want to change like this:您可以做的是,级联父组件并在子组件中,访问您要更改的父属性,如下所示:

Parent:家长:

<CascadingValue Value="this">
    <Child />
</CascadingValue>

Child:孩子:

[CascadingParameter]
public Parent Parent { get; set; }

.....

private void ChangeParentProperty()
{
    Parent.Property = ....; 
}

Any doubt feel free to ask.有任何疑问随时问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM