title: How to limit the number of items from an observablecollection databinded listbox?
body: <p>Here is how it goes. I have a viewmodel where I call a webclient and populate an observable collection with items. I need to have two different listboxes in two different pages but with the same ItemsSource from the viewmodel. How can I limit the number of items in one of the two listboxes without affect the other? I tried to use .Take(limit) into the viewmodel where the items are being created but that affects both listboxes.</p>
  
  <p>Update</p>
  
  <p>The viewmodel</p>
  
  <pre><code>public class MainViewModel : INotifyPropertyChanged
  {
      public MainViewModel()
      {
          this.Items = new ObservableCollection&lt;ItemViewModel&gt;();
          this.Mainlist = new CollectionViewSource();
  
      }
  
      public ObservableCollection&lt;ItemViewModel&gt; Items { get; private set; }
      public CollectionViewSource Mainlist { get; set; }
  
      void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
      {
          try
          {
             ...............
              //Ignore the dummy data in foreach loop. Just for showcase.
  
              foreach (var item in Items)
              {
                  //Items creation
  
                  this.Items.Add(new ItemViewModel()
                  { LineOne = item });
              }
  
              this.Mainlist.Source = App.ViewModel.Items;
              this.Mainlist.Filter += (s, a) =&gt; 
                  a.Accepted = App.ViewModel.Items.IndexOf((ItemViewModel)a.Item) &lt; 10;
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
  
      } 
  
      .....................
  }
  </code></pre>
  
  <p>On the View's side</p>
  
  <pre><code>    public MainPage()
      {
          InitializeComponent();
          DataContext = App.ViewModel;
          list.ItemsSource = App.ViewModel.Mainlist.View;
      }
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>Another option that I found (without using the <code>CollectionViewSource</code>) is to make a new <code>public ObservableCollection&lt;ItemViewModel&gt; Mainlist { get; private set; }</code> and use one more</p>
  
  <pre><code>            foreach (var item in Items.Take(limit))
              {
                  //Items creation
  
                  this.Mainlist.Add(new ItemViewModel()
                  { LineOne = item });
              }
  </code></pre>
  
  <p>in order to populate the ObservableCollection and bind the listbox to Mainlist. That is working but also I think that is poor practice because in that way I have duplicated data. Any ideas on that?</p>