简体   繁体   中英

C# WPF ViewModel for a class with List

I have a class with a list of items, something like this:

public class Model{
    List<int> Items { get; set; }
}

It uses List, not ObservableCollection, and doesn't have any events to be subscribed to. I need to make a ListBox oneway-bound to this list and a way to remove items one-by-one. So I must write a ViewModel class, a property of which can be oneway-bound to ListBox to show these items and to update the ListBox when the list is changed with RemoveItem method. I don't know how to oneway bind ListBox to this List. If I make a ViewModel class implementing INotifyPropertyChanged the same way I do for string property and a textbox, it doesn't work. Please teach me how to do this without changing the Model class.

You could create a property in the view model that is ObservableCollection and initialize it with the list from the model.

The view model will implement the 'removeFromList' method where it will remove an item from the ObservableCollection property (the view model's property) and afterwards you will remove the same item from the list in the model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM