Blazor、MatBlazor - 如何捕捉 MatSelect 组件的值变化

Blazor, MatBlazor - How to catch the value change of MatSelect component

我在我的项目中使用了 MatBlazor 框架。 在 MatSelect 中,我想捕捉它的值 onchange 事件来做一些其他的工作。 我尝试了一些解决方案,但 onchange 事件尚未触发。

<MatSelect Label="Customer" Value="@customer" ValueChanged="OnChangeCustomer">
    <MatOptionString Value="-1">All</MatOptionString>
    @foreach (var item in customers)
    {
        <MatOption Value="@item.Id">@item.Name</MatOption>
    }                
</MatSelect>

下面是我的 onchange 事件处理程序。但是当 select 下拉列表中的另一个值时它没有触发:

public void OnChangeCustomer(ChangeEventArgs args)
{
    if (args.Value.ToString() != "-1")
        isAccountDropDownListDisabled = false;            
}

谁能帮帮我? 谢谢

您可以参考以下示例来使用 MatSelect 控件:

    <MatSelect Outlined="true" Label="Category" ValueChanged="(string i) => OnChangeCategory(i)"> 
        <MatOptionString Value="-1">All</MatOptionString>
        @foreach (var cat in GetCategories())
        {
            <MatOptionString Value="@cat.Id.ToString()">@cat.Name</MatOptionString>
        }
    </MatSelect>
    <span>@selectedValue</span>

    @code
    {
        public string selectedValue;
        protected List<Customer> GetCategories()
        {
            //return new List<string>() { "AA", "BB" };
            return new List<Customer>() {
                    new Customer(){Id=1001, Name="Tom"},
                    new Customer(){Id=1002, Name="David"},
                    new Customer(){Id=1003, Name="Lucy"}
                };
        }

        protected void OnChangeCategory(string value)
        {
            //do something
            selectedValue = "Selected Value: " + value;
        } 
    }

截图如下:

更多详细信息,请查看 MatSelect 文档。

来自@ZhiLv 的代码运行良好,但如果你想要一个预填充的动态 select 值,它会变得更难。

我花了很多时间试图让它与 MatSelectValue 一起工作,但没有运气。

https://www.matblazor.com/SelectValue

我最终使用一个简单的 MatSelect 和一个 属性 调用我的 onchange event 方法。这是我正确预填充 select 列表的唯一方法。

带有可为空 int 的示例,但您也可以更改为字符串、guid 等。

https://www.matblazor.com/Select#MatSelectGuid

@inject StateContainer StateContainer

<MatSelect Label="Choose threat" @bind-Value="@SelectThreatId" Outlined="true" FullWidth="true">
    @foreach (var item in selectThreats)
    {
        <MatOption TValue="int?" Value="@item.Id">@item.Threat</MatOption>
    }
</MatSelect>

@code
    {

    [Parameter]
    public ThreatAndCountermeasureDto ThreatAndCountermeasureDto { get; set; }

    List<ThreatDto> selectThreats = new List<ThreatDto>();

    ThreatDto selectedThreat = null;

    private int? threatId = null;

    public int? SelectThreatId
    {
        get { return threatId; }
        set
        {
            threatId = value;
            SelectThreatValueChanged(value);
        }
    }
    
    private void SelectThreatValueChanged(int? id)
    {
        selectedThreat = StateContainer.Threats.Single(x => x.Id == id);
    }
    
    protected override void OnInitialized()
    {
        base.OnInitialized();
        StateContainer.OnChange += StateHasChanged;
        SelectThreatId = ThreatAndCountermeasureDto.Threat.Id;
        selectThreats = StateContainer.Threats.ToList();
    }
    ...


来源:

https://github.com/SamProf/MatBlazor/issues/498