WPF DataGrid automatically generates line numbers

Source: Internet
Author: User
Tags wpf datagrid

When you use WPF for application development, you often generate line numbers for the DataGrid, and here are the main ways to generate line numbers. There are usually three ways to introduce two of them, and the other is simply to mention them.

1. Operate directly in the Loadingrow event.

This approach is done in the code behind file. That is, the corresponding *.xaml.cs file.

The code is as follows:

This.dataGridSoftware.LoadingRow + = new Eventhandler<datagridroweventargs> (this. Datagridsoftware_loadingrow);

// ...

private void Datagridsoftware_loadingrow (object sender, DataGridRowEventArgs e)
{
E.row.header = E.row.getindex () + 1;
}

This is the most concise and easy to understand approach.

But now many applications are developed using the MVVM (Model-view-modelview) development model. This mode is usually for better decoupling, so usually does not include code in the behind file, in order to implement the above automatic line number operation in this way, you can use the following to refer to the second method. But I personally think that not too inflexible to use MVVM, for generating line numbers this demand, not the scope of business logic, but the scope of the view, so put in the code behind file can also.

2. As mentioned at the end of the first method, in order to adapt to the development model of MVVM, do not want to put the automatic generation line number of the operation into the code behind file to implement, but also want to put into the viewmodel to implement. You can use the method that binds the command to the event, as shown in the following code:

The following namespace is introduced in the design page, and the DLL exists after you install Blend, and the directory is C:\Program Files\Microsoft Sdks\expression\blend\. Netframework\v4.0\libraries, if not, you can download to the Internet.

Xmlns:i= "Clr-namespace:system.windows.interactivity;assembly=system.windows.interactivity"

Setting EventTrigger for the DataGrid, CommandParameter binds the DataGrid itself, which is the parameter that takes the DataGrid as a command.

<datagrid x:name= "Datagridallusers" ...>
<i:interaction. triggers> <i:eventtrigger eventname= "Loaded" >
<i:invokecommandaction command= "{Binding Path=datagridloadedcommand}"
Commandparameter= "{Binding elementname=datagridallusers}" >
</i:InvokeCommandAction>
</i:EventTrigger>
</i:interaction. Triggers></datagrid>

The code in ViewModel, the implementation of Datagridloadedcommand:

Private ICommand Datagridloadedcommand;

Public ICommand Datagridloadedcommand
{
Get
{
if (This.datagridloadedcommand = = null)
{
This.datagridloadedcommand = new Relaycommand (
param =
{
Get the passed DataGrid.
System.Windows.Controls.DataGrid DataGrid = (System.Windows.Controls.DataGrid) param;

After loaded, change all the row header to asending number.
foreach (Var v in Datagrid.items)
{
System.Windows.Controls.DataGridRow dgr = (System.Windows.Controls.DataGridRow) DataGrid.ItemContainerGenerator.ContainerFromItem (v);
if (DGR! = null)
{
Dgr. Header = dgr. GetIndex () + 1;
}
Else
{
At this point, the loaded to the DataGrid, so it would be null
Once you scroll the DataGrid, it begins loading, and it'll trigger the Loadingrow event
As we registered in following code lines, the line number is generated automatically.
Break
}
}

Rgister the Loadingrow event.
Datagrid.loadingrow + = (sender, E) = = {E.row.header = E.row.getindex () + 1;};
});
}

return this.datagridloadedcommand;
}
}

Because the Loadingrow event is registered after the loaded event, the data loaded at the beginning does not have a line number. So think about whether to bind other events is better, nor found. If you find, welcome to share. In order to display line numbers after the DataGrid is loaded, you need to loop through all the rows of the DataGrid and then modify the Datagridrow.header property, which is what the foreach statement in command does. It is also important to note that if the DataGrid has a lot of data that is not displayed completely (with scrollbars) in the viewable range, the DataGrid loads only the visible range of data (items), and the other data is loaded when the scrollbar is dragged for viewing, but its Items collection property contains all item , so there is more than one if statement in foreach to determine, if the DataGridRow is empty, indicating that the visual range of the line number has been updated, you can terminate the loop, register the Loadingrow event. When other items are loaded, the event is automatically triggered to change the line number.

Although this way can realize the function of automatic generation line number, but give people feel also uncomfortable, after all, still operate the concrete control in the ViewModel.

3. The third method is to add an index column to the collection when generating a data source for the DataGrid. This column is also updated when the data source changes, which I didn't try and felt more troublesome.

Finally, you might think that if, in the second method, the parameters of the Loadingrow event can be used as arguments to the command, then the implementation of the command in ViewModel could be as simple as the first method. Here is an example of how to implement command binding in MVVM to get event parameter EventArgs, as a reference: http://blog.csdn.net/qing2005/article/details/6680047

WPF DataGrid automatically generates line numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.