I am trying to update a Blazor ListBox dynamically as per the code listed below. When I do this the Single item selection does not work. Also if I select just the first item on the list and then reload the list (Click button BBB for example) the list box selects the same item in the new list.
@page "/Test1Page"
@using Syncfusion.Blazor.Data;
@using Syncfusion.Blazor.DropDowns;
@using Syncfusion.Blazor.Buttons;
@using Microsoft.Extensions.Logging
@inject ILogger<Test1Page> Logger
@using System.Collections.ObjectModel
@using System.Data;
@using System.Linq;
@attribute [Authorize]
<h3>Test Page 1</h3>
<SfButton OnClick="Button1OnClick">AAA</SfButton>
<SfButton OnClick="Button2OnClick">BBB</SfButton>
<SfButton OnClick="Button3OnClick">CCC</SfButton>
<SfListBox DataSource="@Data" TValue="string[]" TItem="string">
<ListBoxSelectionSettings Mode="Syncfusion.Blazor.DropDowns.SelectionMode.Single" ></ListBoxSelectionSettings>
</SfListBox>
@code {
public List<string> Vehicles = new List<string> { "AAA Hennessey Venom", "AAA Bugatti Chiron", "AAA Bugatti Veyron Super Sport", "AAA SSC Ultimate Aero", "BBB Koenigsegg CCR", "BBB McLaren F1", "BBB Aston Martin One- 77", "CCC Jaguar XJ220", "CCC Holden SV6" };
public static ObservableCollection<string> Data = new ObservableCollection<string>();
public void Button1OnClick()
{
Data.Clear();
foreach (var data in Vehicles.Where(s => s.StartsWith("AAA")))
Data.Add(data);
Logger.LogInformation($"Button 1 click data updated");
}
public void Button2OnClick()
{
Data.Clear();
foreach (var data in Vehicles.Where(s => s.StartsWith("BBB")))
Data.Add(data);
Logger.LogInformation($"Button 2 click data updated");
}
public void Button3OnClick()
{
Data.Clear();
foreach (var data in Vehicles.Where(s => s.StartsWith("CCC")))
Data.Add(data);
Logger.LogInformation($"Button 3 click data updated");
}
}