title: ReactiveUI 9: binding lists to a WPF view
body: <p>In ReactiveUI 9, <code>ReactiveList</code> has been deprecated in favor of DynamicData (<a href="https://reactiveui.net/blog/2018/08/deprecated-reactive-list" rel="noreferrer">Blog post</a>). I am currently in the process of trying to update my code to use <code>SourceList</code>. I was able to get the ViewModel to work, however it seems that using <code>SourceList</code> as a binding datasource in WPF is not as easy.</p>
  
  <p>My first attempt was to create the binding as was done in previous versions of ReactiveUI:</p>
  
  <pre><code>this.OneWayBind(ViewModel, vm =&gt; vm.MyList, v =&gt; v.MyListView.ItemsSource);
  </code></pre>
  
  <p>This doesn't work, because <code>SourceList</code> is not enumerable (Can't convert DynamicData.ISourceList to System.Collections.IEnumerable)</p>
  
  <p>My second attempt was to use the <code>Items</code> property of the list.</p>
  
  <pre><code>this.OneWayBind(ViewModel, vm =&gt; vm.MyList.Items, v =&gt; v.MyListView.ItemsSource);
  </code></pre>
  
  <p>This doesn't work because the <code>Items</code> getter internally creates a copy of the list, which means changes in the list won't be reflected in the view.</p>
  
  <p>My third attempt was to use the <code>Bind</code> method to create a <code>ReadOnlyObservableCollection</code>. 
  I don't want to do this in the viewmodel, because then I would have to add a second list property for every list in every viewmodel which clutters up my code, violating the DRY principle. Furthermore, the type of list to bind to depends on the view framework that is used. (for example: WinForms uses <code>BindingList</code> instead) </p>
  
  <p>Also, the viewmodel of my view might change, which means that the resulting binding and list must be cleaned up and replaced when a new viewmodel is set. This gives me the following snippet:</p>
  
  <pre><code>this.WhenAnyValue(v =&gt; v.ViewModel.VisibleInputs)
      .Select(l =&gt;
      {
          var disposer = l.Connect().Bind(out var list).Subscribe();
          return (List: list, Disposer: disposer);
      })
      .PairWithPreviousValue()
      .Do(p =&gt; p.OldValue.Disposer?.Dispose()) // Cleanup the previous list binding
      .Select(p =&gt; p.NewValue.List)
      .BindTo(this, v =&gt; v.InputsList.ItemsSource);
  </code></pre>
  
  <p>This works fine, but its pretty verbose. I could create an extension method for this, but maybe there is a better/built-in way to do WPF list binding with DynamicData?</p>