简体   繁体   English

WPF datagrid中每一行的任意标题内容

[英]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. 我找到了一种绑定到datagridrow的方法,如下所示,但是我找不到如何获取datagridrow对象的行索引。

<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. 编辑:我使用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. http://code.google.com/p/artur02/source/browse/trunk/DataGridExtensions/DataGridHelper.cs )在AutoGeneratedColumns事件中,我使用GetRow函数设置每行的标题。

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. 它一点也不整洁,但是您可以编写一个IValueConverter,将DataGrid本身作为转换器参数,将当前行作为值(两者都可以通过相对绑定获取),然后查找并返回索引当前行。

I'm sure there's a better approach though... 我相信有更好的方法了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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