我正在使用 MudBlazor 创建一个组件以在 MudSelect 中选择多个项目。当我预填充值时,它们不会出现在选择控件中。当控件展开时,正确的项目被指示为选中。如果我修改他们确实显示的选择。如果我关闭扩展而不进行更改,他们不会。
我需要他们显示初始状态。
我在这里有一个代码演示: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);
}
}
}
东西.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 }
}