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.goog开发者_开发技巧le.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...
精彩评论