绑定:输入框失去焦点,才刷新显示
<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[] { };
}