微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 MultiSelection="true" 在 MudBlazor MudSelect 中显示默认值?

如何解决如何使用 MultiSelection="true" 在 MudBlazor MudSelect 中显示默认值?

我正在使用 MudBlazor 创建一个组件来选择 MudSelect 中的多个项目。当我预填充值时,它们不会出现在选择控件中。当控件展开时,正确的项目被指示为已选择。如果我修改选择,它们会显示。如果我关闭扩展而不做任何更改,他们不会。

我需要它们显示在初始状态。

视频:https://share.clickup.com/clip/p/t1280802/25e76e23-189d-4696-a795-8640b31a798f/screen-recording-2021-03-30-09%3A52.webm

我这里有一个代码演示:https://try.mudblazor.com/snippet/mEmPkHHkpwkPNrkt

__Main.razor:

@using BlazorRepl.UserComponents

<MyComponent Config="MyLocations" 
    OnConfigChanged="LocationsChanged"></MyComponent>

@code {
    private List<Location> MyLocations;
    private List<Location> ModifiedLocations;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        MyLocations = new List<Location>() {Location.inside,Location.underwater};
    }

    private void LocationsChanged(List<Location> val)
    {
        ModifiedLocations = val;
    }
}

MyComponent.razor:

@using BlazorRepl.UserComponents

<MudPaper Elevation="3">
    <MudSelect Label="Locations" SelectedValues="new HashSet<Location>(Config)"
        T="Location" MultiSelection="true" >
        @foreach (Location val in Enum.GetValues(typeof(Location)))
        {
            <MudSelectItem Value="val" />
        }
    </MudSelect>
</MudPaper>

@code {
    [Parameter] public List<Location> Config {get; set;}
    [Parameter] public EventCallback<List<Location>> OnConfigChanged {get; set;}

    private void SelectedValuesChanged(HashSet<Location> val) {
        var v = new List<Location>(val);
        if (!Utilities.EnumListEquals(v,Config))
        {
            Config = v;
            OnConfigChanged.InvokeAsync(Config);
        }        
    }
}

stuff.cs:

namespace BlazorRepl.UserComponents
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    public static class Utilities {
        public static bool EnumListEquals<T>(List<T> one,List<T> two) where T : struct,IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("T must be an enumerated type");
            }
            return (one.Count() == two.Count() &&
                    one.Count(two.Contains) == two.Count());
        }
    }

    public enum Location { inside,outside,underwater }
}

解决方法

这是 MudBlazor 中的一个错误。将在下一版本 MudBlazor v5.0.8 中修复

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。