SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录

接上一篇,本节我们演示如何删除DataGrid中的记录.
功能描述:我们提供一个删除按钮,点击它可以删除当前选中的数据记录行,当然,我们也可以用Delete键盘键删除选中的记录。
一、界面修改
如同添加新记录一样,首先修改用户界面.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="10"/>
            
<Button x:Name="deleteButton" Content="Delete" Margin="10"/>

        
</StackPanel>
        
<data:DataGrid x:Name="dgPeople" Grid.Row="1" />
    
</Grid>

</UserControl>
新用户界面如下图:                       

                        
二、删除按钮点击事件和键盘Delete键事件

事先要绑定事件到DataGrid控件上
            this.deleteButton.Click += new RoutedEventHandler(deleteButton_Click);
            
this.dgPeople.KeyDown += new KeyEventHandler(peopleDataGrid_KeyDown);
1、删除按钮点击事件
  #region 通过按钮删除记录
        
void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            DeletePerson();
        }
        
#endregion
2、Delete键事件
   处理键盘响应事件
3、建立DeletePerson()代码
  #region 删除记录子程序
        
private void DeletePerson()
        {
            
if (null == this.dgPeople.SelectedItem)
            {
                
return;
            }
            Person person 
= this.dgPeople.SelectedItem as Person;
            
if (null == person)
            {
                
return;
            }
            mypeople.Remove(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
{
    
public partial class Page : UserControl
    
{
        People mypeople;

        
public Page()
        
{
            InitializeComponent();

            
this.addButton.Click += new RoutedEventHandler(addButton_Click);
            
this.deleteButton.Click += new RoutedEventHandler(deleteButton_Click);
            
this.dgPeople.KeyDown += new KeyEventHandler(peopleDataGrid_KeyDown);

            Loaded 
+=new RoutedEventHandler(Page_Loaded);


        }


        
private void Page_Loaded(object sender, RoutedEventArgs e)
        
{
            
取得数据源数据并绑定到DataGrid控件上

        }



        
通过按钮添加新记录行



        
通过按钮删除记录



        
删除记录子程序


        
处理键盘响应事件

    }

}

按F5运行测试即可看到效果
                        
posted @ 2009-09-11 07:59  wsdj  阅读(1075)  评论(1编辑  收藏  举报