Click here to Skip to main content
15,072,391 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add my list view items to a array in WPF ? How can i do that?

I added items to list view in this way.


this.ListView1.Items.Add(new MyItem { MenuId = product_id, name = cmb_pname.Text, unitprice = val1, quantity = Convert.ToInt32(txtSaleQty.Text), total = Convert.ToInt32(txt_totalamount.Text) });


But I want to create a array to get the values to that every time I add them to list view.

What I have tried:

this.ListView1.Items.Add(new MyItem { MenuId = product_id, name = cmb_pname.Text, unitprice = val1, quantity = Convert.ToInt32(txtSaleQty.Text), total = Convert.ToInt32(txt_totalamount.Text) });
Posted
Updated 31-May-21 4:56am

1 solution

Add your view models to an array of MyItem. Initialize the array in any way you want.

Set the listview ItemsSource to your array. The list view will not show the items from the array.

If your collection of items can change, use an ObservableCollection<myitem> instead of the array. If some other code add/remove items, your UI will update automatically.

In general I strongly recommend you learn about MVVM. There are plenty of tutorials if you google it. Some will recommend frameworks, others won't. But what is important is you understand the concept.

Using WPF without MVVM just sets you up for a lot of complexity. It can work without MVVM, but it was never designed to do so.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900