WPF中DataGrid前面加上序号

时间:2014-12-25 18:17:16   收藏:0   阅读:1954

首先在XAML页面中的DataGrid中添加一列

<DataGridTemplateColumn Header=" 序号" HeaderStyle="{StaticResource DataGridColumnHeaderStyle1}" Width="50" MinWidth="10" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}, Path=Header}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"></TextBlock>

</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

然后在DataGrid上添加一个LoadingRow事件,该事件的处理方法如下

private void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = e.Row.GetIndex() + 1;
}

原文:http://www.cnblogs.com/zhangabo/p/4185127.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!