简单的Silverlight增 删 改

    工作不是很忙    一时无聊   看到大家都在讨论Silverlight    所以从网上找了个例子研究了一下    看看到底是何方神圣了    当然以我这种水平只能看看简单的    入入门  了解一下   好东西不敢独享    有兴趣的不妨看看

 

    首先新建一个Silverlight应用程序,命名为:SLApplicationExample   然后新添加两个类,分别命名为 Person和Peop

    

 

  1   Person类的定义代码如下:


 using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel; //因为要用到INotifyPropertyChanged接口

namespace SLApplicationExample
{
    
public class Person:INotifyPropertyChanged
    {
        
private string _name;
        
private string _sex;
        
private int _age;
        
private string _address;   

        
#region Constructors   
        
public Person() {    }  

        
public Person(string NameStr, string SexStr, int AgeInt, string AddressStr) 
        {
            
this._name = NameStr;
            
this._sex = SexStr;
            
this._age = AgeInt;
            
this._address = AddressStr;    
        }   
        
#endregion     


        
#region Properties   

        
public string Name 
        {       
            
get { return _name; }      
            
set 
            {          
                
if (value == _name) return;           
                _name 
= value;            
                OnPropertyChanged(
"Name");        
            }   
        }     

        
public string Sex 
        {        
            
get { return _sex; }   
            
set 
            {          
                
if (value == _sex) return;   
                _sex 
= value;        
                OnPropertyChanged(
"Sex");       
            }   
        }    

        
public int Age 
        {      
            
get { return _age; }       
            
set 
            {         
                
if (value == _age) return;       
                _age 
= value;         
                OnPropertyChanged(
"Age");       
            }   
        }   
  
        
public string Address 
        {      
            
get { return _address; }    
            
set 
            {          
                
if (value == _address) return;        
                _address 
= value;
                OnPropertyChanged(
"Address");        
            }   
        }    
        
#endregion    

        
#region INotifyPropertyChanged 接口实现
        
public event PropertyChangedEventHandler PropertyChanged;   
        
protected void OnPropertyChanged(string name) 
        {        
            
if (PropertyChanged != null)      
                PropertyChanged(
thisnew PropertyChangedEventArgs(name));    
        }   
        
#endregion
 2  People类的定义代码如下:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel; //引入此空间,因为要用到ObservableCollection类

namespace SLApplicationExample
{
    
//ObservableCollection表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。 

    
public class People : ObservableCollection<Person>
    {
        
public static People GetTestData()
        {
           People peopleData 
= new People()
          {
            
new Person("Jack""Male"38"ChengDu"),
            
new Person("Marge""Female"33"SpringVale"),
            
new Person("Tom""Male"8"Toorak"),
            
new Person("Simon""Male"21"RingWood"),
            
new Person("Abby""Male"18"BoxHill")
           };
            
return peopleData;
        }
    }
 
 3、用户界面设计,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 >

 

 

 

此用户界面非常简单,就是加入了一个DataGrid控件,我们将在会把我们定义的People类数据源绑定到此控件中进行展示。
  至此,应用程序如下图:
                        
  4、后台代码取数据并完成数据绑定,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)
        {
            mypeople 
=  People.GetTestData();
            
this .dgPeople.ItemsSource  =  mypeople;
        }
         #region 通过按钮添加新记录行
        
void addButton_Click(object sender, RoutedEventArgs e)
        {
            mypeople.Add(
new Person());
        }
        
#endregion
 
         #region 通过Insert键添加新记录行
        
void peopleDataGrid_KeyDown(object sender, KeyEventArgs e)
        {
            
if (Key.Insert == e.Key)
            {
                mypeople.Add(
new Person());
            }
        }
        
#endregion
        
        #region 通过按钮删除记录
        
void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            DeletePerson();
        }
        
#endregion
        

         #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
      

 5  生成项目后运行程序即可看到效果图,如下:

 

 

 

     大功告成   更加详细的参考:http://www.cnblogs.com/wsdj-ITtech/archive/2009/09/11/1564659.html

 



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值