Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I use lazily constructed, auto-updating collections:</p> <pre><code>public class BasketModelView { private readonly Lazy&lt;ObservableCollection&lt;AppleModelView&gt;&gt; _appleViews; public BasketModelView(BasketModel basket) { Func&lt;AppleModel, AppleModelView&gt; viewModelCreator = model =&gt; new AppleModelView(model); Func&lt;ObservableCollection&lt;AppleModelView&gt;&gt; collectionCreator = () =&gt; new ObservableViewModelCollection&lt;AppleModelView, AppleModel&gt;(basket.Apples, viewModelCreator); _appleViews = new Lazy&lt;ObservableCollection&lt;AppleModelView&gt;&gt;(collectionCreator); } public ObservableCollection&lt;AppleModelView&gt; Apples { get { return _appleViews.Value; } } } </code></pre> <p>Using the following <code>ObservableViewModelCollection&lt;TViewModel, TModel&gt;</code>:</p> <pre><code>namespace Client.UI { using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Diagnostics.Contracts; using System.Linq; public class ObservableViewModelCollection&lt;TViewModel, TModel&gt; : ObservableCollection&lt;TViewModel&gt; { private readonly ObservableCollection&lt;TModel&gt; _source; private readonly Func&lt;TModel, TViewModel&gt; _viewModelFactory; public ObservableViewModelCollection(ObservableCollection&lt;TModel&gt; source, Func&lt;TModel, TViewModel&gt; viewModelFactory) : base(source.Select(model =&gt; viewModelFactory(model))) { Contract.Requires(source != null); Contract.Requires(viewModelFactory != null); this._source = source; this._viewModelFactory = viewModelFactory; this._source.CollectionChanged += OnSourceCollectionChanged; } protected virtual TViewModel CreateViewModel(TModel model) { return _viewModelFactory(model); } private void OnSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: for (int i = 0; i &lt; e.NewItems.Count; i++) { this.Insert(e.NewStartingIndex + i, CreateViewModel((TModel)e.NewItems[i])); } break; case NotifyCollectionChangedAction.Move: if (e.OldItems.Count == 1) { this.Move(e.OldStartingIndex, e.NewStartingIndex); } else { List&lt;TViewModel&gt; items = this.Skip(e.OldStartingIndex).Take(e.OldItems.Count).ToList(); for (int i = 0; i &lt; e.OldItems.Count; i++) this.RemoveAt(e.OldStartingIndex); for (int i = 0; i &lt; items.Count; i++) this.Insert(e.NewStartingIndex + i, items[i]); } break; case NotifyCollectionChangedAction.Remove: for (int i = 0; i &lt; e.OldItems.Count; i++) this.RemoveAt(e.OldStartingIndex); break; case NotifyCollectionChangedAction.Replace: // remove for (int i = 0; i &lt; e.OldItems.Count; i++) this.RemoveAt(e.OldStartingIndex); // add goto case NotifyCollectionChangedAction.Add; case NotifyCollectionChangedAction.Reset: Clear(); for (int i = 0; i &lt; e.NewItems.Count; i++) this.Add(CreateViewModel((TModel)e.NewItems[i])); break; default: break; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload