Based on the previous article, we need to further add new records to the DataGrid Control.
Function Description: Add a button above the DataGrid Control. When you click it, a new row is added to the last row of the DataGrid data row. You can edit the new row. In addition, we can add a new row through the insert button on the keyboard.
The specific implementation is as follows:
1. Modify the interface.
Page. XAML Code As follows:
< Usercontrol xmlns: Data = " CLR-namespace: system. Windows. controls; Assembly = system. Windows. Controls. Data " X: Class = " Slapplicationexample. Page "
Xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/presentation "
Xmlns: x = " Http://schemas.microsoft.com/winfx/2006/xaml "
Width = " 400 " Height = " 300 " >
< Grid X: Name = " Layoutroot " Background = " White " Margin = " 5 " >
< Grid. rowdefinitions >
< Rowdefinition height = " Auto " />
< Rowdefinition height = " * " />
</ Grid. rowdefinitions >
< Stackpanel Orientation = " Horizontal " >
< Button X: Name = " Addbutton " Content = " Add " Margin = " 5 " />
</ Stackpanel >
< Data: DataGrid X: Name = " Dgpeople " Grid. Row = " 1 " />
</ Grid >
</Usercontrol>
The interface is as follows:
2. Add button click event and keyboard Insert key event
Bind events to the DataGrid Control beforehand.
This . Addbutton. Click + = New Routedeventhandler (addbutton_click );
This . Dgpeople. keydown + = New Keyeventhandler (leledatagrid_keydown );
1. Button clicking event # Region Click the button to add a new record row
Void Addbutton_click ( Object Sender, routedeventargs E)
{
Mypeople. Add ( New Person ());
}
# Endregion
2. Insert key event # Region Use the Insert key to add a new record row
Void Leledatagrid_keydown ( Object Sender, keyeventargs E)
{
If (Key. insert = E. Key)
{
Mypeople. Add ( New Person ());
}
}
# Endregion
The code for page. XAML. CS is as follows: Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. net;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. documents;
Using System. Windows. input;
Using System. Windows. Media;
Using System. Windows. Media. animation;
Using System. Windows. shapes;
Namespace Slapplicationexample
{
Public Partial Class Page: usercontrol
{
People mypeople;
Public Page ()
{
Initializecomponent ();
This . Addbutton. Click + = New Routedeventhandler (addbutton_click );
This . Dgpeople. keydown + = New Keyeventhandler (leledatagrid_keydown );
Loaded + = New Routedeventhandler (page_loaded );
}
Private Void Page_loaded ( Object Sender, routedeventargs E)
{
obtain data from the data source and bind it to the DataGrid Control # RegionObtain data from the data source and bind it to the DataGrid Control.
Mypeople=People. gettestdata ();
This. Dgpeople. itemssource=Mypeople;
# Endregion
}
Click the button to add a new record row # Region Click the button to add a new record row
Void Addbutton_click ( Object Sender, routedeventargs E)
{
Mypeople. Add (NewPerson ());
}
# Endregion
Use the Insert key to add a new record row # Region Use the Insert key to add a new record row
Void Leledatagrid_keydown ( Object Sender, keyeventargs E)
{
If (Key. insert = E. Key)
{
Mypeople. Add (NewPerson ());
}
}
# Endregion
}
}
The effect after running is as follows:
Go to: Silverlight Study Notes List
This articleProgramIt passes debugging in the silverlight2.0 and vs2008 environments. This article draws on some network materials and hopes to learn from them.
(For more information, see the source)