SilverLight学习笔记--实际应用(一)(2):手把手建立一个Silverlight应用程序之添加记录...

在上一篇的基础上,我们要进一步以实现在DataGrid控件上添加新记录。
实现功能描述:在DataGrid控件上方增加一按钮Add,当点击它时,DataGrid数据行最后一行新增一新行,我们可以编辑此新增的一行。另外,我们也可以通过键盘上的Insert按钮来添加一新行。
具体实现如下:
一、修改界面.
Page.xaml代码如下: 
< 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 >

界面如下:

                        

二、添加按钮点击事件和键盘Insert键事件

事先要绑定事件到DataGrid控件上

             this .addButton.Click  +=   new  RoutedEventHandler(addButton_Click);
            
this .dgPeople.KeyDown  +=   new  KeyEventHandler(peopleDataGrid_KeyDown);
1、按钮点击事件
         #region  通过按钮添加新记录行
        
void  addButton_Click( object  sender, RoutedEventArgs e)
        {
            mypeople.Add(
new  Person());
        }
        
#endregion
2、Insert键事件
         #region  通过Insert键添加新记录行
        
void  peopleDataGrid_KeyDown( object  sender, KeyEventArgs e)
        {
            
if  (Key.Insert  ==  e.Key)
            {
                mypeople.Add(
new  Person());
            }
        }
        
#endregion
Page.xaml.cs全部代码如下:
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
ExpandedBlockStart.gifContractedBlock.gif
{
    
public partial class Page : UserControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        People mypeople;

        
public Page()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
            
this.addButton.Click += new RoutedEventHandler(addButton_Click);
            
this.dgPeople.KeyDown += new KeyEventHandler(peopleDataGrid_KeyDown);
            Loaded
+=new RoutedEventHandler(Page_Loaded);


        }


        
private void Page_Loaded(object sender, RoutedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ContractedSubBlock.gifExpandedSubBlockStart.gif            
取得数据源数据并绑定到DataGrid控件上#region 取得数据源数据并绑定到DataGrid控件上
            mypeople 
= People.GetTestData();
            
this.dgPeople.ItemsSource = mypeople;
            
#endregion


        }

        

ContractedSubBlock.gifExpandedSubBlockStart.gif        
通过按钮添加新记录行#region 通过按钮添加新记录行
        
void addButton_Click(object sender, RoutedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            mypeople.Add(
new Person());
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
通过Insert键添加新记录行#region 通过Insert键添加新记录行
        
void peopleDataGrid_KeyDown(object sender, KeyEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (Key.Insert == e.Key)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                mypeople.Add(
new Person());
            }

        }

        
#endregion


    }

}

运行后效果如下:

                        

前往:Silverlight学习笔记清单
本文程序在Silverlight2.0和VS2008环境中调试通过。本文参照了部分网络资料,希望能够抛砖引玉,大家共同学习。
(转载本文请注明出处)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值