简体   繁体   中英

WPF: GridView with an “index” column

I have a standard ListView with a GridView . Now I want one of the columns in the GridView to be the 1-based index of that line in the list, as such:

| # | First name | Last name |
| 1 | John       | Smith     |
| 2 | Anne       | Anderson  |

How do I do that?

Forgot to answer this one, since the columns are static, you can create an ItemSource binding to the array or whatever is holding the items.

<GridView ItemSource="{Binding ArrayName}" 
          ItemTemplate="{StaticResource gridViewTemplates}" 
          Name="Whatever" ...>
</GridView>

And now we have to set the template

<DataTemplate x:Key="gridViewTemplates">
    <TextBlock Row="{Binding RowNumber}" Column="0" Name="Id" 
           Text={"Binding Id"} />
</DataTemplate>

Please do the changes to whatever suits you best.

More Info on this tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM