SilverLight学习笔记--实际应用(一)(2):手把手建立一个Silverlight应用程序之添加记录...
在上一篇的基础上,我们要进一步以实现在DataGrid控件上添加新记录。
实现功能描述:在DataGrid控件上方增加一按钮Add,当点击它时,DataGrid数据行最后一行新增一新行,我们可以编辑此新增的一行。另外,我们也可以通过键盘上的Insert按钮来添加一新行。
具体实现如下:
一、修改界面.
Page.xaml代码如下:
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(peopleDataGrid_KeyDown);
Loaded+=new RoutedEventHandler(Page_Loaded);


}

private void Page_Loaded(object sender, RoutedEventArgs e)

{

取得数据源数据并绑定到DataGrid控件上#region 取得数据源数据并绑定到DataGrid控件上
mypeople = People.GetTestData();
this.dgPeople.ItemsSource = mypeople;
#endregion

}


通过按钮添加新记录行#region 通过按钮添加新记录行
void addButton_Click(object sender, RoutedEventArgs e)

{
mypeople.Add(new Person());
}
#endregion


通过Insert键添加新记录行#region 通过Insert键添加新记录行
void peopleDataGrid_KeyDown(object sender, KeyEventArgs e)

{
if (Key.Insert == e.Key)

{
mypeople.Add(new Person());
}
}
#endregion

}
}
本文程序在Silverlight2.0和VS2008环境中调试通过。本文参照了部分网络资料,希望能够抛砖引玉,大家共同学习。
(转载本文请注明出处)
实现功能描述:在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>
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、按钮点击事件this.dgPeople.KeyDown += new KeyEventHandler(peopleDataGrid_KeyDown);
#region 通过按钮添加新记录行
void addButton_Click(object sender, RoutedEventArgs e)
{
mypeople.Add(new Person());
}
#endregion
2、Insert键事件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
Page.xaml.cs全部代码如下:void peopleDataGrid_KeyDown(object sender, KeyEventArgs e)
{
if (Key.Insert == e.Key)
{
mypeople.Add(new Person());
}
}
#endregion
运行后效果如下:
本文程序在Silverlight2.0和VS2008环境中调试通过。本文参照了部分网络资料,希望能够抛砖引玉,大家共同学习。
(转载本文请注明出处)
转载于:https://www.cnblogs.com/wsdj-ITtech/archive/2009/09/11/1564470.html