0

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.

3
  • Did you try something like ListView.Items.Add()? Commented Nov 14, 2022 at 21:07
  • Please tag the full platform details! - From the link it looks as if you are using App UI (.NET MAUI) ListView- This seems to be fully data bound and you would have to add the new items to the underlying datasource. Commented Nov 14, 2022 at 21:08
  • -Tags edited. -Yes, I tried with ListView.Items.Add() but then this error shows up: "CS1061 'ListView' does not contain a definition for 'Items' and no accessible extension method 'Items' accepting a first argument of type 'ListView' could be found (are you missing a using directive or an assembly reference?)". -And I am using App UI (.NET MAUI) ListView. Commented Nov 14, 2022 at 21:13

1 Answer 1

3

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.