Blazor在子组件更改父组件值时运行自定义事件

Blazor在子组件更改父组件值时运行自定义事件,blazor,blazor-server-side,Blazor,Blazor Server Side,我有一个父组件和子组件: 父组件 <IncrementButton Increment="-1" @bind-Attribute="Amount" @onclick="@(() => UpdateBottomPillText())" /> <span>@Amount</span> <IncrementButton Increment="1" @bind-Attribute="Amount" @onclick="@(() => UpdateBo

我有一个父组件和子组件:

父组件

<IncrementButton Increment="-1" @bind-Attribute="Amount" @onclick="@(() => UpdateBottomPillText())" />
<span>@Amount</span>
<IncrementButton Increment="1" @bind-Attribute="Amount" @onclick="@(() => UpdateBottomPillText())" />
<span>@bottomPillAmount</span>

@code {
    [Parameter] public int Amount { get; set; }
    [Parameter] public EventCallback<int> AmountChanged { get; set; }

    string bottomPillAmount;

    string UpdateBottomPillText()
    {
        double amountAdded = Math.Floor(((double)(Amount - 10.0)) / 2.0);

        bottomPillAmount = amountAdded > 0 ? "+" + amountAdded.ToString() : amountAdded.ToString();

        return Amount.ToString();
    }

    protected override void OnParametersSet()
    {
        UpdateBottomPillText();
    }

}

@bind Attribute=Amount在按下IncrementButton时起作用并更新值,但我想运行UpdateBottomPillText并更新值。现在,我正在通过onclick事件进行此操作,但是有没有办法修改父组件中的AttributeChanged事件?

这里需要的是适当地设置组件之间的双向数据绑定

递增按钮 用法
这是非编译代码。修复它并复制/粘贴正在工作或显示问题的内容。太棒了,它工作得非常好。我的第二个问题是如何对自定义对象中的属性执行相同的操作?如果你能看一下,我发布了我的问题:
<button type="button" @onclick="OnAttributeChanged">@displayText</button>

@code {
    [Parameter]
    public int Increment { get; set; } = 1;

    [Parameter]
    public int Attribute { get; set; }

    [Parameter]
    public EventCallback<int> AttributeChanged { get; set; }

    [Parameter]
    public EventCallback<MouseEventArgs> OnClick { get; set; }

    string displayText;

    protected override void OnParametersSet()
    {
        displayText = Increment > 0 ? "+" + Increment.ToString() : Increment.ToString();
    }

    private Task OnAttributeChanged()
    {
        Attribute += Increment;
        await OnClick.InvokeAsync(new MouseEventArgs());
        return AttributeChanged.InvokeAsync(Attribute);
    }
}
<button type="button" @onclick="OnAttributeChanged">@displayText</button>

@code {
[Parameter]
public int Increment { get; set; } = 1;

[Parameter]
public int Attribute { get; set; }

[Parameter]
public EventCallback<int> AttributeChanged { get; set; }


string displayText;

protected override void OnParametersSet()
{
    displayText = Increment > 0 ? "+" + Increment.ToString() : 
                                                Increment.ToString();
}

private Task OnAttributeChanged()
{
    Attribute += Increment;
    return AttributeChanged.InvokeAsync(Attribute);
}
}
<IncrementButton Increment="-1" @bind-Attribute="Amount"/>
<span>@Amount</span>
<IncrementButton Increment="1" @bind-Attribute="Amount"/>
<span>@bottomPillAmount</span>

@code {
  private int amount;
  [Parameter] public int Amount
  {
    get => amount;
    set {
        if(amount != value)
        {
            amount = value;
            UpdateBottomPillText();

        }
    }
 }

 string bottomPillAmount;

string UpdateBottomPillText()
{
    double amountAdded = Math.Floor(((double)(Amount - 10.0)) / 2.0);

    bottomPillAmount = amountAdded > 0 ? "+" + amountAdded.ToString() : amountAdded.ToString();

    return Amount.ToString();
}

protected override void OnParametersSet()
{
    UpdateBottomPillText();

}

}