|
<SfListBox DataSource="@Data" TValue="string[]" TItem="ListData">
<ListBoxFieldSettings Text="text" Value="id"></ListBoxFieldSettings>
</SfListBox>
<SfButton CssClass="e-success" @onclick="PropertyBinding">Add</SfButton>
@code
{
public ObservableCollection<ListData> Data { get; set; }
public class ListData
{
public string id { get; set; }
public string text { get; set; }
public static ObservableCollection<ListData> getListData()
{
ObservableCollection<ListData> Data = new ObservableCollection<ListData>();
Data.Add(new ListData() { text = "Hennessey Venom", id = "list-01" });
Data.Add(new ListData() { text = "Bugatti Chiron", id = "list-02" });
Data.Add(new ListData() { text = "Bugatti Veyron Super Sport", id = "list-03" });
Data.Add(new ListData() { text = "SSC Ultimate Aero", id = "list-04" });
Data.Add(new ListData() { text = "Koenigsegg CCR", id = "list-05" });
Data.Add(new ListData() { text = "McLaren F1", id = "list-06" });
Data.Add(new ListData() { text = "Aston Martin One- 77", id = "list-07" });
Data.Add(new ListData() { text = "Jaguar XJ220", id = "list-08" });
Data.Add(new ListData() { text = "McLaren P1", id = "list-09" });
Data.Add(new ListData() { text = "Ferrari LaFerrari", id = "list-10" });
return Data;
}
}
protected override void OnInitialized()
{
this.Data = ListData.getListData();
}
private void PropertyBinding()
{
this.Data.Add(new ListData() { id = "c", text = "RAKKI" });
}
}
|
|
@using Syncfusion.Blazor.DropDowns
@using Syncfusion.Blazor.Buttons
@using System.Collections.ObjectModel
<SfListBox DataSource="@Data" TValue="string[]" TItem="ListData">
<ListBoxFieldSettings Text="Text" Value="Id"></ListBoxFieldSettings>
</SfListBox>
<SfButton CssClass="e-success" @onclick="PropertyBinding">Add</SfButton>
@code
{
private void OnValueChange(ListBoxChangeEventArgs<string[], ListData> args)
{
}
public ObservableCollection<ListData> Data { get; set; }
public class ListData
{
public Guid Id { get; set; }
public string Text { get; set; }
public static ObservableCollection<ListData> getListData()
{
ObservableCollection<ListData> Data = new ObservableCollection<ListData>();
Data.Add(new ListData() { Text = "Hennessey Venom", Id = Guid.NewGuid() });
Data.Add(new ListData() { Text = "Bugatti Chiron", Id = Guid.NewGuid() });
Data.Add(new ListData() { Text = "Bugatti Veyron Super Sport", Id = Guid.NewGuid() });
Data.Add(new ListData() { Text = "SSC Ultimate Aero", Id = Guid.NewGuid() });
Data.Add(new ListData() { Text = "Koenigsegg CCR", Id = Guid.NewGuid() });
Data.Add(new ListData() { Text = "McLaren F1", Id = Guid.NewGuid() });
Data.Add(new ListData() { Text = "Aston Martin One- 77", Id = Guid.NewGuid() });
Data.Add(new ListData() { Text = "Jaguar XJ220", Id = Guid.NewGuid() });
Data.Add(new ListData() { Text = "McLaren P1", Id = Guid.NewGuid() });
Data.Add(new ListData() { Text = "Ferrari LaFerrari", Id = Guid.NewGuid() });
return Data;
}
}
protected override void OnInitialized()
{
this.Data = ListData.getListData();
}
private void PropertyBinding()
{
this.Data.Add(new ListData() { Id = Guid.NewGuid(), Text = "RAKKI" });
}
}
|
I also have this bug, when I remove an item from the list, it doesn't refresh after a StateHasChanged()
I have to make the SfListBox disappear and then reapper
Ugly workaround
Hi Daniel,
We have checked your provided code snippet, and your workaround is different from which we have provided a solution in this forum. Using the ObservableCollection data collection in the Blazor Listbox component, while adding or removing the items from the data source, it will refresh automatically without using the StatehasChanged function. Refer to the below-attached sample code file and video demonstration.
Based on your provided code snippet, you are fetching the data from the service and setting it to the listbox component. So kindly share the below details.
How did you get the data source for the listbox and data type that data source in your project?
Share the simple sample to replicate your issue or share the full-page code snippet with video demonstration.
Based on the above details, we will check and provide a better solution.
Regards,
YuvanShankar A