Search code examples
c#.netlistviewmaui

How to add items to ListView with .NET C#


I want to add items to a ListView with .NET 7 + C#. Is there a way to do this programmatically with C#? The reason is the app will receive data from a server and I want to add parts of the data to the listview.

I have read this https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/listview?view=net-maui-7.0 but there is no information on how to do this with C# completely, neither is there anything I can find on Google (except by installing packages, which I want to avoid). Maybe I am misunderstanding something.

I would appreciate an example.


Solution

  • the ListView gets its data from its ItemsSource, which is an IEnumerable. To add data dynamically, just Add elements to the data source

    ObservableCollection<string> myData = new ObservableCollection<string>();
    
    MyListView.ItemsSource = myData;
    
    ...
    
    myData.Add("new item");