[原]如何在Silverlight中使用WebService绑定DataGrid
Page.xaml

<UserControl x:Class="SilverlightApplication15.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
>
<Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
<data:DataGrid x:Name="dgrd_DataGrid" Margin="5" AutoGenerateColumns="False" >
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="姓名" Width="150"
Binding="{Binding UserName}">
</data:DataGridTextColumn>
<data:DataGridTextColumn Header="年龄" Width="150"
Binding="{Binding UserAge}">
</data:DataGridTextColumn> "
</data:DataGrid.Columns>
</data:DataGrid>
</Grid>
</UserControl>
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;
using System.Collections.ObjectModel;
namespace SilverlightApplication15
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
void GetUser()
{
UserService.Service1SoapClient userService = new SilverlightApplication15.UserService.Service1SoapClient();
userService.GetUserCompleted += new EventHandler<SilverlightApplication15.UserService.GetUserCompletedEventArgs>(userService_GetUserCompleted);
userService.GetUserAsync();
}
void userService_GetUserCompleted(object sender, SilverlightApplication15.UserService.GetUserCompletedEventArgs e)
{
if (e.Error == null)
{
List<UserTest> user = new List<UserTest>();
ObservableCollection<SilverlightApplication15.UserService.User> list = e.Result;
for (int i = 0; i < list.Count; i++)
{
user.Add(new UserTest
{
UserName = list[i].UserName,
UserAge = list[i].UserAge
});
}
this.dgrd_DataGrid.ItemsSource = user;
}
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
GetUser();
}
}
}
WebService中仅包含一个返回List<User>的GetUser();
PS:在Silverlight中并不包含DataGrid,如需使用,请引用System.Windows.Controls.Data
并且在Page.xaml中声明
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
分类:
Silverlight
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
2007-06-13 ASP.NET2.0轻松搞定统计图表[转载]
2007-06-13 asp.net2.0导出pdf文件完美解决方案[转载]
2007-06-13 GridView 72般绝技[转载]
2007-06-13 asp.net利用RAR实现文件压缩解压缩[转载]