asp.net - 如何动态更改 blazor 组件

我需要更改列表中的项目,该列表是一个显示数字的组件,但它的数字取决于名为 component2 的其他组件,如果 component2 更改她的 value component1 应该重置并更改为新数字。 component2 是另一个列表。

这是组件 1:

<div>
    <div class="d-flex listbox-box" @onclick="DropDown" style="height:@(height)px !important; width:@(width)px !important;padding:0px;">

        @if (ItemIsSelected)
        {
            <IterationItem height="@height" width="@width" _Iteration="Iteration[SelectedIndex]" />
        }else{
        <div class="align-self-center" style="width:100%;">
            <div class="float-end">
                <div class=" icon-container" style="transform: rotate(@(rotation)deg) !important;">
                    <i class="bi bi-chevron-compact-down icon-listbox"/>
                </div>
            </div>
        </div>
        }


      </div>
    @if (ShowDropDown && Iteration != null)
    {
            <div class="example dropdown-listbox" style="width:@(width)px !important;height:@(height * 3)px !important">
            @for (int i = 0; i < Iteration.Length-1; i++)
            {
                var index = i;
                        <div id="@i" value="@SelectedIndex" @onclick='eventargs => HandleValueChange(eventargs,index)'>
                            <IterationItem height="@height" width="@width" _Iteration="Iteration[i]" />
                        </div>
                        <hr class="listbox-item-separator"/>
            }
            </div>
    }
</div>

@code {
    int rotation = 0;
    [Parameter] public int SelectedIndex { get; set; } = -1;
    [Parameter] public bool ShowDropDown { get; set; } = false;
    [Parameter] public bool ItemIsSelected { get; set; } = false;
    [Parameter] public Iteration[] Iteration { get; set; }
    public int height { get; set; } = 40;
    public int width { get; set; } = 120;


    private void DropDown()
    {
        ShowDropDown = ShowDropDown ? false : true;

        if (ShowDropDown)
        {
            rotation = 180;
        }
        else
        {
            rotation = 0;
        }
    }

    protected void HandleValueChange(MouseEventArgs evt, int index)
    {
        SelectedIndex = index;
        DropDown();
        ItemIsSelected = true;

    }


}

这是交换数字的事件:

void ClickHandler(int num)
    {
        _modelValue = num;
        _selectedModel =   
     XqueryFunctions.CreatrModelById(_modelValue,"");
    }

这就是我现在试图改变它的方式:

<ListBoxIteration Iteration="@_selectedModel.Iterations" />

@code {
    private string ProyectName { get; set; } = "Fred";
    int _modelValue;
    Model _selectedModel = new ();
    void ClickHandler(int num)
    {
        _modelValue = num;
        _selectedModel =   
        XqueryFunctions.CreatrModelById(_modelValue,"");
    }
}

回答1

这段代码有效,我有其他问题。我应该删除这个帖子吗?

相似文章

c# - 子组件在父组件之前初始化

我已经实现了一个状态容器,以便父页面将状态传递给2个子组件,子组件可以更新状态并且父组件会知道这些更改。这是我的状态容器(它被注册为单例并注入到父组件和子组件中):publicclassEditSta...

随机推荐

最新文章