Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Personally, I use an ObservableCollection in my model and my viewmodel.</p> <pre><code>class Model { public ObservableCollection&lt;Foo&gt; Foos; } class ViewModel { public Model Model; public ObservableCollection&lt;FooView&gt; Foos; public ViewModel() { Model.Foos.CollectionChanged += OnModelFoosCollection_CollectionChanged; } void OnModelFoosCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { Foo re; switch (e.Action) { case NotifyCollectionChangedAction.Add: re = e.NewItems[0] as Foo; if (re != null) AddFoo(re); //For other logic that may need to be applied break; case NotifyCollectionChangedAction.Remove: re = e.OldItems[0] as Foo; if (re != null) RemoveFoo(re); break; case NotifyCollectionChangedAction.Reset: Foos.Clear(); /* I have an AddRange in an ObservableCollection-derived class You could do Model.Foo.ForEach(ree =&gt; AddFoo(ree)); */ var converter = from ree in Model.Foo select new FooView(ree); Reports.AddRange(converter); break; default: //exercise for the reader :) s_ILog.Error("OnModelFoosCollection_CollectionChangedDid not deal with " + e.Action.ToString()); break; } } void AddFoo(Foo f) { Foos.Add(new FooView(f)); } void RemoveFoo(Foo f) { var match = from f in Foos where f.Model == f //if you have a unique id, that might be a faster comparison select f; if(match.Any()) Foos.Remove(match.First()); } } </code></pre> <p>Now, when you remove something from the Model's Foo collection, it will automatically remove the corresponding FooView. This corresponds with how I think about this sort of thing. If I want to delete something, the Model is where is really needs to be deleted.</p> <p>It feels like a lot of code, but it isn't that much, really. I'm sure one could build a generic version of this, but IMO you always end up wanting custom logic dealing with addition/removal of elements.</p>
    singulars
    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. 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