【问题标题】:Which event in a MudBlazor MudSelect is called when selection is changed and how would it be implemented?Which event in a MudBlazor MudSelect is called when selection is changed and how would it be implemented?
【发布时间】:2022-12-21 21:12:19
【问题描述】:

我正在使用 MudBlazor 并按照文档实现了一个 MudSelect 组件。

但是,当做出选择但不确定要调用哪个事件时,我试图从 MudSelect 中获取选定的值。 Tried a few such as SelectedValuesChanged but nothing is firing in my code block when the selection has been updated.

使用标准的 HTML 选择,我只需调用 @onchange,然后为该事件编写一个方法。这在 MudBlazor 中不起作用。

这是我的 MudSelect

<MudSelect T="Stage" Label="Stage" Variant="Variant.Filled" AnchorOrigin="Origin.BottomCenter">
    <MudSelectItem Value="@(new Stage("Stage 1"))" />
    <MudSelectItem Value="@(new Stage("Stage 2"))" />
    <MudSelectItem Value="@(new Stage("Stage 3"))" />
    <MudSelectItem Value="@(new Stage("Stage 4"))" />
    <MudSelectItem Value="@(new Stage("Stage 5"))" />
</MudSelect>

这是@code块

public class Stage
{
    public Stage(string stageName)
    {
        StageName = stageName;
    }

    public readonly string StageName;

    public override bool Equals(object o)
    {
        var other = o as Stage;
        return other?.StageName == StageName;
    }

    public override int GetHashCode() => StageName?.GetHashCode() ?? 0;

    public override string ToString() => StageName;
}

【问题讨论】:

    标签: c# blazor .net-6.0 mudblazor


    【解决方案1】:

    您可以使用 ValueChanged 事件回调,它在 Value 属性更改时触发。

    执行:

    <MudSelect T="Stage" 
            ToStringFunc="@converter"
            ValueChanged="OnValueChanged" 
            Label="Stage" 
            Variant="Variant.Filled" 
            AnchorOrigin="Origin.BottomCenter">
        <MudSelectItem Value="@(new Stage("Stage 1"))" />
        <MudSelectItem Value="@(new Stage("Stage 2"))" />
        <MudSelectItem Value="@(new Stage("Stage 3"))" />
        <MudSelectItem Value="@(new Stage("Stage 4"))" />
        <MudSelectItem Value="@(new Stage("Stage 5"))" />
    </MudSelect>
    
    @if(selectedStage is not null)
    {
        <br/>
        <MudAlert Severity="Severity.Info">@(selectedStage.StageName) was selected</MudAlert>
    }
    
    @code {
        private Stage selectedStage {get; set;}
        readonly Func<Stage, string> converter = p => p.StageName;
    
        private void OnValueChanged(Stage selected)
        {
            selectedStage = selected;
            // Do other stuff
        }
    
        public class Stage
        {
            public Stage(string stageName)
            {
                StageName = stageName;
            }
    
            public readonly string StageName;
    
            public override bool Equals(object o)
            {
                var other = o as Stage;
                return other?.StageName == StageName;
            }
    
            public override int GetHashCode() => StageName?.GetHashCode() ?? 0;
    
            public override string ToString() => StageName;
        }
    }
    

    演示:
    https://try.mudblazor.com/snippet/cOwGFYQavuiuzWnd

    输出:

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2022-12-26
      • 2022-12-02
      • 1970-01-01
      • 2018-12-07
      • 2022-12-01
      • 2022-12-02
      • 2022-12-02
      • 2022-12-16
      相关资源
      最近更新 更多