title: WPF MVVM Pagination
body: <p>I have a user control for pagination view, it has its own viewmodel. I added the pagination in a page with a datagrid that also has a separate viewmodel.</p>
  
  <p>My question is how can I update the ObservableCollection I have in my page viewmodel every time a command is done in my pagination viewmodel?</p>
  
  <p>Here's my PagingControl.xaml </p>
  
  <pre><code>&lt;StackPanel Width="Auto"
          Orientation="Horizontal"&gt;
          &lt;Button                
              Margin="4,0"
              Content="&amp;lt;&amp;lt;"
              Command="{Binding FirstCommand}"/&gt;
          &lt;Button
              Margin="4,0"
              Content="&amp;lt;"
              Command="{Binding PreviousCommand}"/&gt;
          &lt;StackPanel
              VerticalAlignment="Center"
              Orientation="Horizontal"&gt;
              &lt;TextBlock
                  Text="{Binding Start}"/&gt;
              &lt;TextBlock
                  Text=" to "/&gt;
              &lt;TextBlock
                  Text="{Binding End}"/&gt;
              &lt;TextBlock
                  Text=" of "/&gt;
              &lt;TextBlock
                  Text="{Binding TotalItems}"/&gt;
          &lt;/StackPanel&gt;
          &lt;Button
              Margin="4,0"
              Content="&amp;gt;"
              Command="{Binding NextCommand}"/&gt;
          &lt;Button
              Margin="4,0"
              Content="&amp;gt;&amp;gt;"
              Command="{Binding LastCommand}"/&gt;
          &lt;ComboBox Width="100" ItemsSource="{Binding ItemsPerPage}" SelectedValue="{Binding ItemCount}"&gt;
              &lt;i:Interaction.Triggers&gt;
                  &lt;i:EventTrigger EventName="SelectionChanged"&gt;
                  &lt;i:InvokeCommandAction Command="{Binding CountChangedCommand}"/&gt;
                  &lt;/i:EventTrigger&gt;
              &lt;/i:Interaction.Triggers&gt;
          &lt;/ComboBox&gt;
      &lt;/StackPanel&gt;
  </code></pre>
  
  <p>PagingViewModel.cs</p>
  
  <pre><code>public class PagingViewModel : ViewModelBase
  {
      private ObservableCollection&lt;DataModel&gt; _data;
  
      private int start = 0;
      private int itemCount = 10;
      private int totalItems = 0;
      private readonly List&lt;int&gt; count;
  
      private ICommand _firstCommand;
      private ICommand _previousCommand;
      private ICommand _nextCommand;
      private ICommand _lastCommand;
      private ICommand _countchangedCommand;
  
      public ObservableCollection&lt;DataModel&gt; Data
      {
          get { return _data; }
          set
          {
              if (_data!= value)
              {
                  _data= value;
                  OnPropertyChanged("Data");
              }
          }
      }
  
      public PagingViewModel()
      {
          count = new List&lt;int&gt; { 10, 20, 30};
          RefreshData();
      }
  
      public int Start { get { return start + 1; } }
  
      public int End { get { return start + itemCount &lt; totalItems ? start + itemCount : totalItems; } }
  
      public int TotalItems { get { return totalItems; } }
  
      public List&lt;int&gt; Count { get { return count; } }
  
      public int ItemCount { get { return itemCount; } set { itemCount = value; OnPropertyChanged("ItemCount"); } }
  
      public ICommand FirstCommand
      {
          get
          {
              if (_firstCommand == null)
              {
                  _firstCommand = new RelayCommand
                  (
                      param =&gt;
                      {
                          start = 0;
                          RefreshData();
                      },
                      param =&gt;
                      {
                          return start - itemCount &gt;= 0 ? true : false;
                      }
                  );
              }
  
              return _firstCommand;
          }
      }
  
      public ICommand PreviousCommand
      {
          get
          {
              if (_previousCommand == null)
              {
                  _previousCommand = new RelayCommand
                  (
                      param =&gt;
                      {
                          start -= itemCount;
                          RefreshData();
                      },
                      param =&gt;
                      {
                          return start - itemCount &gt;= 0 ? true : false;
                      }
                  );
              }
  
              return _previousCommand;
          }
      }
  
      public ICommand NextCommand
      {
          get
          {
              if (_nextCommand == null)
              {
                  _nextCommand = new RelayCommand
                  (
                      param =&gt;
                      {
                          start += itemCount;
                          RefreshData();
                      },
                      param =&gt;
                      {
                          return start + itemCount &lt; totalItems ? true : false;
                      }
                  );
              }
  
              return _nextCommand;
          }
      }
  
      public ICommand LastCommand
      {
          get
          {
              if (_lastCommand == null)
              {
                  _lastCommand = new RelayCommand
                  (
                      param =&gt;
                      {
                          start = (totalItems / itemCount - 1) * itemCount;
                          start += totalItems % itemCount == 0 ? 0 : itemCount;
                          RefreshData();
                      },
                      param =&gt;
                      {
                          return start + itemCount &lt; totalItems ? true : false;
                      }
                  );
              }
  
              return _lastCommand;
          }
      }
  
      public ICommand CountChangedCommand
      {
          get
          {
              if (_countchangedCommand == null)
              {
                  _countchangedCommand = new RelayCommand
                  (
                      param =&gt;
                      {
                          start = 0;
                          RefreshData();
                      },
                      param =&gt;
                      {
                          return ((totalItems - itemCount) &gt; -10) ? true : false;
                      }
                  );
              }
  
              return _countchangedCommand;
          }
      }
  
      public void RefreshData()
      {
          _data= GetData(start, itemCount, out totalItems);
  DataViewModel vm = new DataViewModel(this);
  
          OnPropertyChanged("Start");
          OnPropertyChanged("End");
          OnPropertyChanged("TotalItems");
      }
  }
  </code></pre>
  
  <p>And here's the viewmodel for my Page: DataViewModel.cs</p>
  
  <pre><code>public class DataViewModel: ViewModelBase
  {
      private ObservableCollection&lt;DataModel&gt; _data;
  
      public ObservableCollection&lt;DataModel&gt; Data
      {
          get { return _data; }
          set
          {
              if (_data!= value)
              {
                  _data= value;
                  OnPropertyChanged("Data");
              }
          }
      }
  
      public DataViewModel(PagingViewModel pagevm)
      {
          _data = new ObservableCollection&lt;DataModel&gt;();
          _data= pagevm.Data; 
      }
  
  }
  </code></pre>
  
  <p>My Data property is bound to an ItemSource of a DataGrid in DataView.xaml with DataContext set to DataViewModel.</p>