CrabMan
CrabMan

Reputation: 1758

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.

Upvotes: 0

Views: 475

Answers (1)

Dennis Nerush
Dennis Nerush

Reputation: 5663

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.

Upvotes: 1

Related Questions