WPF DataGrid control is a simple method of automatically generated numbers - after deleting still ordered
Simple method of automatically generated DataGrid control number
Number generation DataGrid control are generally two methods, the idea is to create a method of the first number listed in a separate control and Binding with background data. The second method is not the Binding , in direct line to load RowHeader add serial number. In contrast, the second method is more simple, so here mainly to introduce the second method.
FIG effect
reception codes Xaml
<DataGrid x:Name="TCurveDataGrid" Height="280" Width="150"
AlternatingRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False"
BorderBrush="Black" BorderThickness="1" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
LoadingRow="TCurveDataGrid_LoadingRow"
HeadersVisibility="All">
<DataGrid.Columns>
<DataGridTextColumn x:Name="Parameter" Header="参数" Binding="{Binding Parameter,UpdateSourceTrigger=PropertyChanged}" Width="75" />
<DataGridTextColumn x:Name="Value" Header="数值" Binding="{Binding Value,UpdateSourceTrigger=PropertyChanged}" Width="75"/>
</DataGrid.Columns>
</DataGrid>
Background C # code
//在载入行的时候在行表头添加编号
private void TCurveDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}
The above methods present problems
Since when performing the Delete operation to the DataGrid control does not trigger LoadingRow event, it will be deleted after the disorder.
Type Delete before
you type Delete
The final solution
Add PreviewKeyUp events and establish a method to update you type Delete items control contained in the event handler.
Reception Xaml Code
<DataGrid x:Name="TCurveDataGrid" Height="280" Width="150"
AlternatingRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False"
BorderBrush="Black" BorderThickness="1" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
LoadingRow="TCurveDataGrid_LoadingRow" PreviewKeyUp="TCurveDataGrid_PreviewKeyUp"
HeadersVisibility="All">
<DataGrid.Columns>
<DataGridTextColumn x:Name="Parameter" Header="参数" Binding="{Binding Parameter,UpdateSourceTrigger=PropertyChanged}" Width="75" />
<DataGridTextColumn x:Name="Value" Header="数值" Binding="{Binding Value,UpdateSourceTrigger=PropertyChanged}" Width="75"/>
</DataGrid.Columns>
</DataGrid>
Background C # code
//在载入行的时候在行表头添加编号
private void TCurveDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}
//在键入delete后,刷新控件所包含的item以保持表头序号的连续。
private void TCurveDataGrid_PreviewKeyUp(object sender, KeyEventArgs e)
{
if (TCurveDataGrid.SelectedIndex < 0) return;
var dgr = (DataGridRow)(TCurveDataGrid.ItemContainerGenerator.ContainerFromIndex(TCurveDataGrid.SelectedIndex));
if (e.Key == Key.Delete && !dgr.IsEditing)
{
TCurveDataGrid.Items.Refresh();
}
}
Show results
Type Delete before
you type Delete
The problem is not a problem
The last line of the row header defaults to 2, I think it is possible to list the first default and WPF for 1, row header defaults to 2 related, but do not know how should such as how to replace other symbols, such as *, hoping to get pointing.