在列表中添加序号列

在列表中添加序号,序号随着列的增加而增加,同时又不会影响到ItemSource的赋值,即不需在列表绑定的类中添加一个绑定序号的字段。


定义类:

 1  public class NumColumn : Telerik.Windows.Controls.GridViewColumn
 2     {
 3         public override FrameworkElement CreateCellElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem)
 4         {
 5             TextBlock textBlock = cell.Content as TextBlock;
 6 
 7             if (textBlock == null)
 8             {
 9                 textBlock = new TextBlock();
10             }
11 
12             textBlock.Text = (this.DataControl.Items.IndexOf(dataItem) + 1).ToString();
13 
14             return textBlock;
15         }
16 
17     }

然后在xaml中引用这个类:

xmlns:local="clr-namespace:CSNMSSL.csnms.controls(命名空间)"

 <telerik:RadGridView ShowGroupPanel="False" AutoGenerateColumns="False"
                         IsReadOnly="True" IsFilteringAllowed="False" 
                         RowIndicatorVisibility="Collapsed" CanUserFreezeColumns="False" CanUserSelect="True"
                         RowStyle="{StaticResource TssGridViewRowStyle}" x:Name="cutList" Grid.Row="0" FontFamily="Arial"  FontSize="12" >
                        <telerik:RadGridView.Columns>
                            <local:NumColumn Header="序号" Width="40"/>
原文地址:https://www.cnblogs.com/lh-V/p/3422749.html