title: WPF CollectionView/DataGrid get index of first visible item
body: <p>is there a way to get the first visible item that is displayed to the user?</p>
  
  <p>In the WinForms's <code>DatagridView</code> we had <code>FirstDisplayedScrollingRowIndex</code>.
  Is there an equivalent for the WPF variant?</p>
  
  <p>I'm using a CollectionView in my ViewModel which is bound to a DataGrid in XAML.</p>
  
  <blockquote>
    <p><strong>Just to be clear:</strong> I <strong>don't</strong> want to get the SelectedRow's index, this I
    can already do...</p>
  </blockquote>
  
  <p><strong>Example</strong><br>
  I've got 20 items in my ObservableCollection and my Datagrid can only show 13 due to size-restrictions.
  Item2 was previously selected by the user and after that the user scrolled a bit down, so items 5-17 are visible.
  How do I get the index of item5?</p>
  
  <p><strong>XAML</strong>  </p>
  
  <pre><code>&lt;Style x:Key="DatagridStyle" TargetType="DataGrid"&gt;
          &lt;Setter Property="AutoGenerateColumns" Value="False"/&gt;
          &lt;Setter Property="Background" Value="{StaticResource ColorDatagridBackground}"/&gt;
          &lt;Setter Property="IsReadOnly" Value="True"/&gt;
          &lt;Setter Property="CanUserAddRows" Value="False"/&gt;
          &lt;Setter Property="CanUserDeleteRows" Value="False"/&gt;
          &lt;Setter Property="CanUserResizeColumns" Value="False"/&gt;
          &lt;Setter Property="CanUserReorderColumns" Value="True"/&gt;
          &lt;Setter Property="CanUserSortColumns" Value="True"/&gt;
          &lt;Setter Property="ColumnHeaderHeight" Value="25"/&gt;
          &lt;Setter Property="Margin" Value="0,5,0,5"/&gt;
          &lt;Setter Property="ItemsSource" Value="{Binding ItemCollection}"/&gt;
  &lt;/Style&gt;
  &lt;DataGrid DockPanel.Dock="Top" 
                Style="{StaticResource DatagridStyle}"
                util:DataGridColumnsBehavior.BindableColumns="{Binding DatagridColumns, UpdateSourceTrigger=PropertyChanged}"
                IsSynchronizedWithCurrentItem="True"
                EnableRowVirtualization="True"&gt;
          &lt;i:Interaction.Behaviors&gt;
              &lt;util:DataGridScrollBehaviour /&gt;
          &lt;/i:Interaction.Behaviors&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>private ObservableCollection&lt;DataGridColumn&gt; _datagridColumns;
  private CollectionView _itemCollection;
  private CollectionViewSource _itemCollectionSource;
  public ObservableCollection&lt;DataGridColumn&gt; DatagridColumns
      {
          get =&gt; _datagridColumns;
          set
          {
              _datagridColumns = value;
              RaisePropertyChanged();
          }
      }
      public CollectionView ItemCollection
      {
          get =&gt; _itemCollection;
          set
          {
              _itemCollection = value;
              RaisePropertyChanged();
          }
      }
      public CollectionViewSource ItemCollectionSource
      {
          get =&gt; _itemCollectionSource;
          set
          {
              _itemCollectionSource = value;
              RaisePropertyChanged();
          }
      }
  
  
      _datagridColumns = MainViewModel.GetColumns(MainViewModel.AppMode.Match);
  
      _itemCollectionSource = new CollectionViewSource();
      ItemCollectionSource.Source = _vml.Main.ItemList;
      _itemCollection = (CollectionView)ItemCollectionSource.View;
  </code></pre>
  
  <p><strong>Desired Result:</strong>
  if my view-area is ...<br>
   - at the <strong>top</strong> and I add an item at the top (due to current sorting), I want my view-area to stay at the top, so I can see my new item<br>
   - at the <strong>bottom</strong> and I add an item at the bottom (due to current sorting), I want my view-area to move to the 'new' bottom, so I can see my new item<br>
   - is anywhere in the middle, I want to keep looking at the same X items</p>
  
  <p>I can access the SortOrder via my CollectionView, but in order to determine where my view-area needs to be moved, I do need to know, where my view-area currently is (top, middle, bottom)</p>