0
votes

Recently, I have been trying to implement the MVVM design pattern, but I encountered 2 problems which I can't solve:

  1. As I see it, I must use ObservableCollection in my Model classes, in order to pass it in the ModelView to the View. I hope that I wrong, because the View must not affect the Model structure, and I shouldn't be limited to this specific collection type.

  2. Is there any way to do two-way binding with a value-type item list?

Example:

public ObservableCollection<bool> MyBooleans
{
    get { return m_booleans; }
}
<ListView ItemsSource="{Binding MyBooleans}" ...>
    <ItemTemplate>
        ...
        <CheckBox IsChecked="{Binding}" ... />
        ...
    </ItemTemplate>
</ListView>
1
1. No, see Dependency Properties, INotifyCollectionChanged, INotifyPropertyChanged. 2. Not without a wrapper. - user7116
Please read the formatting help. - H.B.

1 Answers

8
votes

Your view model should expose collections which change (i.e. have items added / removed) as ObservableCollections (or some other class that implements INotifyCollectionChanged). This does not mean your model needs to expose collection that implement this interface.

Your view model is effectively an adapter on your model that makes it more readily bindable. As an example, if your application displays tweets, your service layer might return a model which is a list of tweets. Your view model would then insert these into an observable collection, resulting in your view being updated. You could then retrieve new tweets via your service at some point in the future (using a timer), these again would be returned as a list. Your view model would then add these tweets to its ObservableCollection resulting in the new items being visible in the view.