0

I have a View and a Viewmodel.

The Viewmodel has a property of type ICollectionView which I instance a CollectionViewSource with the Source, which is an ObservableCollection<Foo>

Now on my view I have a Datagrid to show this info, and in my Datagrid.Columns I have a DataGridTemplateColumn, inside a DataGridTemplateColumn.HeaderTemplate and then a DataGridTemplateColumn.CellTemplate

Now inside my cell template for my 1st header, I want to show the index or element number of each element of my collection, the cell template is like this:

                <DataGridTemplateColumn.CellTemplate>
                    <ItemContainerTemplate>
                        <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path= DataContext.CurrentPosition}"></TextBlock>
                    </ItemContainerTemplate>
                </DataGridTemplateColumn.CellTemplate>

Now my DataContext.CurrentPosition returns empty, I dont know if this is the correct property of the object to return the index.

Nickso
  • 785
  • 1
  • 10
  • 32
  • Are you refering to [this property](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.icollectionview.currentposition?view=netframework-4.8#System_ComponentModel_ICollectionView_CurrentPosition)? If this is the case, then you can only get the index of the current element of the `CollectionViewSource` itself and not get the individual index of each element. – Corentin Pane Feb 05 '20 at 15:13

1 Answers1

0

Ok I have solved the issue by following Andy's answer in this thread: How to show row-number in first column of WPF Datagrid

Nickso
  • 785
  • 1
  • 10
  • 32