I'm using MudBlazor to create a component to select multiple items in a MudSelect. When I pre-populate values they don't appear in the select control. When the control is expanded the correct items are indicated as selected. If I modify the selection they do show. If I close the expansion without making changes they don't.
I need them to show on the initial state.
I have a code demo here: 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 }
}