简体   繁体   中英

Arbitrary Header content for each row in WPF datagrid

I'm trying to put row header text based on a converter function of the index of the row. I found a way to bind to the datagridrow like below, but i can't find how to get the row index of the datagridrow object.

<DataTemplate x:Key="MyRowHeaderTemplate" DataType="DataRowView">
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1,0,1,0" Padding="1,0,1,0"
               Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}, Path=Item}"/>
</DataTemplate>

How can i get the row index and use my converter function to set the row header text?

EDIT: i solved this using some helper extension of datagrid. ( http://code.google.com/p/artur02/source/browse/trunk/DataGridExtensions/DataGridHelper.cs ) On the AutoGeneratedColumns event, i set the each row's header using the GetRow function.

Its not at all neat, but you could write an IValueConverter that takes in the DataGrid itself as a converter parameter and the current row as the value (both of which could be acquired through relative binding) and then look up and return the index of the current row.

I'm sure there's a better approach though...

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