Silverlight learning notes-Practical Application (1) (2): Create a Silverlight application with your hands to add records

Source: Internet
Author: User
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)

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.