您的位置:首页 > 其它

Silverlight 3 DataGrid简单数据绑定

2009-11-20 11:23 447 查看
 新建一个Link To SQL类:



设置序列化模式:



新建一个web服务:代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace SilverlightApplication8.Web
{
/// <summary>
/// CustomerService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class CustomerService : System.Web.Services.WebService
{
[WebMethod]
public List<Customers> CustomerDetails()
{
CustomersDataContext customerContext = new CustomersDataContext();
return customerContext.Customers.ToList();

}
}
}


添加web服务:



右击:MainPage.xaml 选择在Expression Blend 中打开,拖一个DataGrid控件。代码如下

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightApplication8.MainPage"
d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<data:DataGrid Margin="54,40,23,190" x:Name="datagrid" AutoGenerateColumns="True" ItemsSource="{Binding}"
>
</data:DataGrid>
</Grid>
</UserControl>


在MainPage.xaml.cs代码添加如下代码:

using System;
using System.Windows.Controls;
using SilverlightApplication8.CustomerService;
using System.Windows.Data;
namespace SilverlightApplication8
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
CustomerService.CustomerServiceSoapClient customerClient =
new CustomerService.CustomerServiceSoapClient();
customerClient.CustomerDetailsAsync();
customerClient.CustomerDetailsCompleted += new EventHandler<CustomerDetailsCompletedEventArgs>(customerClient_CustomerDetailsCompleted);

}

void customerClient_CustomerDetailsCompleted(object sender, CustomerDetailsCompletedEventArgs e)
{
if (e.Error == null)
{
datagrid.ItemsSource = e.Result;
}
}

}
}


 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息