I want to create a components by following this steps:
- 我有一张商品 list .
- I want to loop in this list and create a component like
InputNumber
.
- 将
EventCallback
添加到接受这Inputtext
中的ref
的泛型创建的InputNumber
中,因为我想使用这ref
来将焦点设置在这InputNumber
上. - I have also
onblure
method that execute some code for me, and I am using theonfocus
to return focus to the input after execute this code byonblure
My question
How can I get this ref
and send it as parameter of EventCallback
?
The problem here that this components have been generated by loop, so I don't want to create by hand hundred variables to represent ref's
.
My concept code like this:
@code{
private void OnFocus(MyInputNumber<double?> obj)
{
if (obj is not null)
{
obj!.Element.Value.FocusAsync();
}
}
}
@foreach(var MyItem in MyList)
{
<EditForm Model="MyItem">
//Some components ..
<label>
Test
<InputNumber @bind-Value="MyItem.MyVal"
@onfocus="@((InputNumber<double?> obj @*wrong*@) =>
OnFocus(obj))"
@onblur=@(() => OnblureHandler(context))
</label>
</EditForm>
}
If you see up the parameter InputNumber<double?> obj
, this way is wrong, usually I use @ref=SomeVariable
but becasue I created in generic way, I can not do that.
Note:
我不需要将我的列表调整为dictionary<MYItemType,InputNumber<double?>>
,或者创建一个将InputNumber<double?>
作为属性的新类.我正在寻找不同的方式,比如从editcontext
到任何输入都被修改并重新设置焦点,我不知道这是否可能!