views:

204

answers:

3

I am trying to understand MVVM pattern and I took a sample program to work on. The model is a C# .net library which when queried returns an IList object. I want my view to be able to add/edit/delete items in this collection thru' data binding. But I don't know how to start with this, using MVVM. Please help me out.

Model exposes an interface to retrieve the IList objecs, View has a list box showing the contents of IList and couple of other controls to add data to the IList

+1  A: 

My blog post on DelegateCommand should give you a good grounding.

HTH,
Kent

Kent Boogaart
+3  A: 

Bind your ListBox to an ObservableCollection<T> and it will instantly be updated everytime you add or remove something from that ObservableCollection<T>.

This ObservableCollection<T> should normally be a property of your ViewModel.

If the IList property from your Model is not observable (and you don't have control over it), you will have to write the code to synchronize it within your ViewModel. These are questions of architecture then. I feel the cleanest choice in this regard is to actually use a ReadOnlyObservableCollection<T> and add / remove items using your repository and synchronize accordingly.

herzmeister der welten
Is there a way to automate this synchronization? Like we bind ObservableCollection to the control, is there a way to bind the IList from my library to the ObservableCollection<T> in my ViewModel?
sudarsanyes
Seems you can't wrap a normal collection into an ObservableCollection anymore like in ye olde days. ;-)The brute force method would not be complicated, just clear the list and read it from your model again.More elegant approaches would be a custom observable collection class that leverages some HashSet-like algorithms.
herzmeister der welten
A: 

I bind my list box to an ObservableCollection<Item> and I added/removed/modified items into and out of this collection. It works fine. Check this out on how it works without any INotifyPropertyChanged. Please correct me if I am wrong

sudarsanyes