Hello,
I'm trying to update the text of a ListView item, but I can't get the UI to update. Below is my code. Similar code works with DataGrid as seen in the documentation. Is there a way for force a refresh? StateHasChanged doesn't work, unfortunately. Also, I would like the same thing to work with ComboBox. Any help is greatly appreciated.
@page "/listbox"
@using Syncfusion.Blazor.DropDowns
@using System.Collections.ObjectModel;
@using System.ComponentModel;
<SfListBox @bind-Value=@value DataSource="@allData" TValue="string" TItem="MyData">
<ListBoxFieldSettings Text="Name" Value="Guid"></ListBoxFieldSettings>
</SfListBox>
<button @onclick="update">Update first row</button>
@code
{
string value;
ObservableCollection<MyData> allData;
protected override async Task OnInitializedAsync()
{
allData = new ObservableCollection<MyData>();
allData.Add(new MyData() { Name = "Peter" });
allData.Add(new MyData() { Name = "Bob" });
allData.Add(new MyData() { Name = "Susan" });
allData.Add(new MyData() { Name = "Paul" });
value = allData.First().Guid;
}
async Task update()
{
var first = allData.First();
first.Name = "Michael";
InvokeAsync(StateHasChanged);
}
class MyData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Guid { get; set; } = System.Guid.NewGuid().ToString();
string _name = "NEW";
public string Name
{
get => _name;
set
{
_name = value;
NotifyPropertyChanged(nameof(Name));
}
}
void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Hi Tomi,
We have checked your reported issue and prepared the sample based on your requirement. kindly refer to the below code snippet and attached sample file.
public ObservableCollection<VehicleData> Vehicles { get; set; } public class VehicleData : INotifyPropertyChanged { public string text { get; set; } public string Text { get { return text; } set { this.text = value; NotifyPropertyChanged("DataSource"); } } public string Id { get; set; } public static ObservableCollection<VehicleData> getListData() { ObservableCollection<VehicleData> data = new ObservableCollection<VehicleData>(); data.Add(new VehicleData() { Text = "Hennessey Venom", Id = "Vehicle-01" }); data.Add(new VehicleData() { Text = "Bugatti Chiron", Id = "Vehicle-02" }); data.Add(new VehicleData() { Text = "Bugatti Veyron Super Sport", Id = "Vehicle-03" }); data.Add(new VehicleData() { Text = "SSC Ultimate Aero", Id = "Vehicle-04" }); data.Add(new VehicleData() { Text = "Koenigsegg CCR", Id = "Vehicle-05" }); data.Add(new VehicleData() { Text = "McLaren F1", Id = "Vehicle-06" }); data.Add(new VehicleData() { Text = "Aston Martin One- 77", Id = "Vehicle-07" }); data.Add(new VehicleData() { Text = "Jaguar XJ220", Id = "Vehicle-08" }); return data; } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
private void modifyData() { var item = Vehicles.FirstOrDefault(i => i.Id == "Vehicle-06"); if (item != null) { item.Text = "Smith"; } } |
Get back to us if you need any further assistance on this.
Regards,
YuvanShankar A
Hello,
Thank you. This works great.
You are welcome, Tomi. Please get back to us if you need any other assistance.
If that post is helpful, please consider accepting it as the solution so that other members can locate it more quickly.