思路:
在每个datagrid的第一列加上一个模板列,里面只定义一个textblock
之后在datagrid的行加载事件中,给每个行的textblock加上一个编号。
xaml代码:
<sdk:DataGridTemplateColumn CanUserSort="False" Header="序号"> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"/> </DataTemplate> </sdk:DataGridTemplateColumn.CellTemplate> </sdk:DataGridTemplateColumn>
cs代码:
// 加载控件值 /// <summary> /// 加载控件值 /// </summary> private void InitControl() { try { #region 控件赋值 this.OtherTechnique_Infos_Schema = null; this.tbTEXT.Text = string.Empty; this.dgList.ItemsSource = null; this.dgList.ItemsSource = this.listOtherTechnique_Infos_Schemas.OrderBy(t => t.CODE); this.dgList.LoadingRow += dataGrid_LoadingRow; #endregion 控件赋值 } catch (Exception) { throw; } } /// <summary> /// 序号 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e) { var index = e.Row.GetIndex(); var cell = this.dgList.Columns[0].GetCellContent(e.Row) as TextBlock; if (cell != null) cell.Text = (index + 1).ToString(); }
大功告成!