语法备忘03:bind、绑定

绑定:输入框失去焦点,才刷新显示

<input @bind="x"  />

x=@x

@code {
    private int x = 1;
}

事件绑定:实时刷新显示

<input @bind="x" @bind:event="oninput" />

x=@x

@code {
    private int x = 1;
}

自定义绑定

<input @bind:event="oninput" @bind:get="x" @bind:set="OnInput" />

@code {
    string? x;
    void OnInput(string value)
    {
        var newValue = value ?? string.Empty;
        x = newValue.Length > 4 ? "Long!" : newValue;
    }

}

自定义绑定:参数写法

    private void OnInput(ChangeEventArgs args)
    {
        var newValue = args.Value?.ToString() ?? string.Empty;
        x = newValue.Length > 4 ? "Long!" : newValue;
    }

列表多选绑定

        <select @bind="SelectedCities" multiple>
            <option value="bal">Baltimore</option>
            <option value="la">Los Angeles</option>
            <option value="pdx">Portland</option>
            <option value="sf">San Francisco</option>
            <option value="sea">Seattle</option>
        </select>

@code {
    string[] SelectedCities { get; set; } = new string[] { };
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值