如何使用 MultiSelection="true" 使默认值出现在 MudBlazor MudSelect 中?
How do I get default values to appear in MudBlazor MudSelect with MultiSelection="true"?
我正在使用 MudBlazor 创建一个组件以 select MudSelect 中的多个项目。当我预填充值时,它们不会出现在 select 控件中。当控件展开时,正确的项目将显示为 selected。如果我修改 selection,它们就会显示出来。如果我不做任何更改就关闭扩展,他们不会。
我需要它们在初始状态下显示。
我这里有一个代码演示: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
中修复
我正在使用 MudBlazor 创建一个组件以 select MudSelect 中的多个项目。当我预填充值时,它们不会出现在 select 控件中。当控件展开时,正确的项目将显示为 selected。如果我修改 selection,它们就会显示出来。如果我不做任何更改就关闭扩展,他们不会。
我需要它们在初始状态下显示。
我这里有一个代码演示: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
中修复