C# WPF ViewModel for a class with List

只谈情不闲聊 提交于 2019-12-08 11:03:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/21972981/c-sharp-wpf-viewmodel-for-a-class-with-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!