c# - Append to Global List -
i started learning wpf , c#. have global list want append on event.
code:
public partial class mainwindow : window { public list<user> items = new list<user>(); someeventhandler { items.add(new user() { name = "john", age = 42 }); listviewusers.itemssource = items; } }
however, updated first time event fires, , not subsequent events. if move
public list<user> items = new list<user>();
to someeventhandler, listviewusers show latest update , not previous record. how append items?
although prefer bind in xaml via viewmodel, solve problem, should use observablecollection
below. automatically update listview if item added in collection:
public partial class mainwindow : window { public observablecollection<user> items = new observablecollection<user>(); public mainwindow() { initializecomponent(); listviewusers.itemssource = items; } someeventhandler { items.add(new user() { name = "john", age = 42 }); } }
Comments
Post a Comment