KeyboardFriendly
KeyboardFriendly

Reputation: 1798

Display row numbers in Wpf Listview

I have a listview that is being created from a sql server database, how do you include the row numbers?

            <ListView x:Name="lstName" Height="400" Canvas.Top="55" Width="450">
                <ListView.View>
                    <GridView>                     
                        <GridViewColumn Header="Number" Width="auto" 
                         DisplayMemberBinding="{Binding Path=Id}" />
                        <GridViewColumn Header="FirstName" Width="auto" 
                         DisplayMemberBinding="{Binding Path=FName}" />
                        <GridViewColumn Header="LastName" Width="auto" 
                         DisplayMemberBinding="{Binding Path=LName}" />
                        <GridViewColumn Header="StreetAddress" Width="auto" 
                         DisplayMemberBinding="{Binding Path=Street}" />
                        <GridViewColumn Header="City" Width="auto" 
                         DisplayMemberBinding="{Binding Path=City}" />
                        <GridViewColumn Header="State" Width="auto"
                         DisplayMemberBinding="{Binding Path=State}" />                       
                    </GridView>                        
                </ListView.View>
            </ListView>

Upvotes: 1

Views: 4075

Answers (3)

Suhel Khan
Suhel Khan

Reputation: 1

Its a very old post and I have seen many people asked how we can avoid zero-based index with AlternationCount and AlternationIndex. So for other users like me searching a solution. You can do it with a bit unusual(or you can say weird) workaround but its working for me:

Hide the first item row (Status is Model.Status):

<ListView.ItemContainerStyle>
  <Style TargetType="ListViewItem">
    <Style.Triggers>
      <DataTrigger Binding="{Binding Status}" Value="Status">
        <Setter Property="Visibility" Value="Collapsed" />
      </DataTrigger>
     </Style.Triggers>
 </Style>
 </ListView.ItemContainerStyle>

After that add a dummy record in first row:

FileDetail fd = new FileDetail()
{
  FileName = "File Name",
  Count = "Count",
  Status = "Status",
  FilePath = "File Path"
};
FileList.Add(fd);

Manage your iteration throughout your code accordingly.

One more thing you can do with that 0 row. You can hide the original header and make this 0 row as a header.

<ListView.Resources>
  <Style TargetType="{x:Type GridViewColumnHeader}">
   <Setter Property="Visibility" Value="Collapsed" />
  </Style>
 </ListView.Resources>

Upvotes: 0

torrential coding
torrential coding

Reputation: 1765

You could set AlternationCount for the ListView to a value greater than the possible number of rows that can be returned. Then, bind to ListView.AlternationIndex to get a zero-based index for the current item.

Upvotes: 2

Qortex
Qortex

Reputation: 7466

I don't see an easy way to do that as WPF philosophy is to bind to data, and then the view should be independent.

But I can see kind of a hack to get it to work.

You bind (OneTime) against a Counter property from your ViewModel, and in the get accessor of this property, you iterate the counter in addition to returning it.

That way, each ListView item will have a proper index.

But be careful with that solution, you won't be able to trace back an index to an item after that. If you need to also do that for whatever reason, you should actually make a IdInTable field in your object and populate it correctly in your ViewModel.

Upvotes: 1

Related Questions