- 浏览: 474048 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (1028)
- [发布至博客园首页] (826)
- [随笔分类]个人生活随笔 (14)
- [网站分类]首页候选区 (26)
- [网站分类]SharePoint (15)
- [网站分类]其他技术区 (6)
- [随笔分类]批处理技巧 (6)
- [随笔分类].net 2.0 (3)
- [随笔分类]SharePoint2007(MOSS2007) (0)
- [网站分类].NET新手区 (6)
- [网站分类]ASP.NET (6)
- [网站分类]架构设计 (18)
- [网站分类]程序人生 (2)
- [网站分类]SQL Server (2)
- WCF (3)
- 编程技巧 (2)
- 模式架构 (2)
- 分析设计 (4)
- 生活随笔 (0)
- 软件工程 (1)
- Android实例 (2)
最新评论
-
zilong0536:
楼主您好:
请问发表博文支持图片的功能怎么实现啊,一直没有思路 ...
新浪微博开放平台开发-android客户端(3) -
nicegege:
小弟 学习了
帮助中国移动设计10086的排队小模块 -
zl7824516:
用什么技术没说啊
通告(公告),消息(站内短信),提醒的设计:通告 -
virusswb:
源码下载: SinaWeibo2 源码下载之后,将后缀改为ra ...
新浪微博开放平台开发-android客户端(3) -
Jimmyxu0311:
找不到源码下载
新浪微博开放平台开发-android客户端(3)
接着上面一篇,我们来讨论绑定集合等。
首先看一下可以进行绑定集合的控件属性,暂时我就不翻译了,因为翻译不好,还不如读英文呢。
Name |
Description |
ItemsSource |
Points to the collection that has all the objects that will be shown in the list. |
DisplayMemberPath |
Identifies the property that will be used to creat the display text for each item. |
ItemTemplate |
Providers a data template that will be used to create the visual appearance of each item.This property acts as a far more powerful replacement for DisplayMemberPath. |
ItemsPanel |
Providers a template that will be used to create the layout container that holds all the items in the list. |
这里你可能会想,什么类型的集合可以绑定到ItemsSource属性呢?告诉你,只需要实现IEnumerable就可以,但是,实现这个借口的集合是只读的。如果你想编辑这个集合,例如允许插入和删除,你需要更多的工作,后面我们会介绍。
显示并且编辑集合项
首先定义个数据库交互契约
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace Domain.Entity
{
[DataContract]
public class Customer : INotifyPropertyChanged
{
private int _intCustomerId;
private string _strCustomerName;
private string _strCustomerCode;
private CustomerType _CustomerType;
private int _intCustomerTypeId;
[DataMember ]
public virtual int CustomerTypeId
{
get { return _intCustomerTypeId; }
set { _intCustomerTypeId = value; }
}
[DataMember ]
public virtual CustomerType CustomerType
{
get { return this._CustomerType; }
set
{
this._CustomerType = value;
OnPropertyChanged("CustomerType");
}
}
[DataMember]
public virtual int CustomerId
{
get { return this._intCustomerId; }
set
{
this._intCustomerId = value;
OnPropertyChanged("CustomerId");
}
}
[DataMember]
public virtual string CustomerName
{
get { return this._strCustomerName; }
set
{
this._strCustomerName = value; OnPropertyChanged("CustomerName");
}
}
[DataMember]
public virtual string CustomerCode
{
get { return _strCustomerCode; }
set
{
this._strCustomerCode = value;
OnPropertyChanged("CustomerCode");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
wcf定义接口
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Domain.Entity;
namespace WcfService
{
[ServiceContract]
public interface IServiceCustomer
{
[OperationContract]
Domain.Entity.Customer GetCustomer(int customerId);
[OperationContract]
IList<Domain.Entity.Customer> GetAll();
[OperationContract]
void Add(Domain.Entity.Customer customer);
[OperationContract]
string SayHello(SysUser sysUser);
}
}
实现这个接口
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Domain.Entity;
using Common.Core;
using Common.Data;
namespace WcfService
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ServiceCustomer : IServiceCustomer
{
private CustomerDAO _customerDao;
Common.Core.Utility.NHibernateUtility _NHUtility;
MyValidator _myValidator;
public ServiceCustomer()
{
_NHUtility = new Common.Core.Utility.NHibernateUtility();
_customerDao = new CustomerDAO(_NHUtility.GetSession());
}
// Add more operations here and mark them with [OperationContract]
#region IServiceCustomer Members
public Domain.Entity.Customer GetCustomer( int customerId)
{
Domain.Entity.Customer objCustomer = new Domain.Entity.Customer();
return _customerDao.GetCustomerById(customerId);
}
#endregion
#region IServiceCustomer Members
public IList<Domain.Entity.Customer> GetAll()
{
IList<Domain.Entity.Customer> cs = _customerDao.GetAll();
return cs;
}
#endregion
#region IServiceCustomer Members
public void Add(Domain.Entity.Customer customer)
{
_customerDao.CreateCustomer(customer);
}
#endregion
#region IServiceCustomer Members
public string SayHello(SysUser sysUser)
{
_myValidator = (MyValidator)OperationContext.Current.Host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator;
return string.Format("hello,{0},your password is {1}\n{2}{3}", sysUser.UserName, sysUser.Password,
// _myValidator.ToString(),_myValidator.ToString() );
_myValidator.UserName ,_myValidator.Password );
}
#endregion
}
}
Silverlight客户端前台代码
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" x:Class="Silverlight.sldbdemo"
Width="400">
<ScrollViewer>
<StackPanel>
<Button x:Name="btnGetCustomer" Content="获取一个用户信息" Click="btnGetCustomer_Click"></Button>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock x:Name="LblCustomerId" Grid.Column="0" Grid.Row="0" Text="Customer Id"/>
<TextBlock x:Name="TxtCustomerId" Grid.Column="1" Grid.Row="0" Text="{Binding CustomerId}"/>
<TextBlock x:Name="LblCustomerCode" Grid.Column="0" Grid.Row="1" Text="Customer Code"/>
<TextBlock x:Name="TxtCustomerCode" Grid.Column="1" Grid.Row="1" Text="{Binding CustomerCode}"/>
<TextBlock x:Name="LblCustomerName" Grid.Column="0" Grid.Row="2" Text="用户名称"/>
<TextBlock x:Name="TxtCustomerName" Grid.Column="1" Grid.Row="2" Text="{Binding CustomerName}"/>
</Grid>
<Button x:Name="btnGetAllCustomer" Content="获取全部用户信息" Click="btnGetAllCustomer_Click"></Button>
<Grid x:Name="customers" Background="Gray" Grid.ColumnSpan="2" Grid.Column="0" Grid.Row="3">
<data:DataGrid x:Name="dataGrid" CanUserResizeColumns="true" CanUserSortColumns="True"
AutoGenerateColumns="False" ItemsSource="{Binding}">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Id" Binding="{Binding CustomerId}"></data:DataGridTextColumn>
<data:DataGridTextColumn Header="Code" Binding="{Binding CustomerCode}"></data:DataGridTextColumn>
<data:DataGridTextColumn Header="Name" Binding="{Binding CustomerName}"></data:DataGridTextColumn>
</data:DataGrid.Columns>
</data:DataGrid>
</Grid>
<StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="4">
<data:DataPager x:Name="tempPager" DisplayMode="FirstLastPreviousNextNumeric" HorizontalAlignment="Left" VerticalAlignment="Top"
Source="{Binding Path=ItemsSource,ElementName=dataGrid}"
PageSize="5"
></data:DataPager>
</StackPanel>
<Grid x:Name="AddCustomer" Background="White" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="5" Height="97">
<Grid.ColumnDefinitions >
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions >
<RowDefinition Height="0.247*" ></RowDefinition>
<RowDefinition Height="0.247*" ></RowDefinition>
<RowDefinition Height="0.247*" ></RowDefinition>
<RowDefinition Height="0.258*" ></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="Customer Id" Grid.Column="0" Grid.Row="0"></TextBlock>
<TextBox x:Name="customerId" Grid.Column="1" Grid.Row="0"></TextBox>
<TextBlock Text="Custoemr Code" Grid.Column="0" Grid.Row="1"></TextBlock>
<TextBox x:Name="customerCode" Grid.Column="1" Grid.Row="1"></TextBox>
<TextBlock Text="Customer Name" Grid.Column="0" Grid.Row="2"></TextBlock>
<TextBox x:Name="customerName" Grid.Column="1" Grid.Row="2"></TextBox>
<Button x:Name="btnOk" Grid.Column="0" Grid.Row="3" Height="25" Content="添加用户Add Customer" Click="btnOk_Click"></Button>
</Grid>
<Button x:Name="btnLoadData" Click="btnLoadData_Click" Content="加载数据"></Button>
<ListBox x:Name="lstCustomers"></ListBox>
</StackPanel>
</ScrollViewer>
</UserControl>
Silverlight客户端后台代码
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 Silverlight.ServiceCustomer;
using Domain.Entity;
namespace Silverlight
{
public partial class sldbdemo : UserControl
{
ServiceCustomerClient client;
SysUser _sysUser;
private int _customerId = 1;
public sldbdemo()
{
InitializeComponent();
client = new ServiceCustomerClient();
client.ClientCredentials.UserName.UserName = "adminstrator";
client.ClientCredentials.UserName.Password = "123.com";
_sysUser = new SysUser() { UserName = "swb", Password = "swb" };
client.SayHelloAsync(_sysUser);
client.SayHelloCompleted += new EventHandler<SayHelloCompletedEventArgs>(client_SayHelloCompleted);
}
void client_SayHelloCompleted(object sender, SayHelloCompletedEventArgs e)
{
try
{
MessageBox.Show(e.Result);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.InnerException.Message);
}
}
protected void GetCustomerById(int customerId)
{
try
{
client.GetCustomerCompleted += new EventHandler<GetCustomerCompletedEventArgs>(client_GetCustomerCompleted);
client.GetCustomerAsync(customerId );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.InnerException.Message);
}
}
void client_GetCustomerCompleted(object sender, GetCustomerCompletedEventArgs e)
{
// Customer customer = new Customer() { CustomerId = 1, CustomerCode = "ss", CustomerName = "dddd" };
//LayoutRoot.DataContext = customer;
LayoutRoot.DataContext = e.Result;
}
protected void LoadCustomers()
{
try
{
client.GetAllCompleted += new EventHandler<GetAllCompletedEventArgs>(client_GetAllCompleted);
client.GetAllAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.InnerException.Message);
}
}
void client_GetAllCompleted(object sender, GetAllCompletedEventArgs e)
{
System.Windows.Data.PagedCollectionView page = new System.Windows.Data.PagedCollectionView(e.Result);
tempPager.Source = page;
dataGrid.ItemsSource = page;
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
try
{
client.AddCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_AddCompleted);
Domain.Entity.Customer customer = new Domain.Entity.Customer()
{
CustomerId = int.Parse(customerId.Text),
CustomerCode = customerCode.Text,
CustomerName = customerName.Text
};
client.AddAsync( customer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.InnerException.Message);
}
}
void client_AddCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
LoadCustomers();
}
private void btnLoadData_Click(object sender, RoutedEventArgs e)
{
client.GetAllAsync();
client.GetAllCompleted +=new EventHandler<GetAllCompletedEventArgs>(client_GetAllCompleted1);
}
void client_GetAllCompleted1(object sender, GetAllCompletedEventArgs e)
{
lstCustomers.ItemsSource = e.Result;
}
private void btnGetCustomer_Click(object sender, RoutedEventArgs e)
{
GetCustomerById(_customerId);
}
private void btnGetAllCustomer_Click(object sender, RoutedEventArgs e)
{
LoadCustomers();
}
}
}
效果图
你会看到后面的listbox中,数据显示的全部都是Domain.Entity.Customer,没有显示成我们想要的某一栏的值,这就是因为在代码中我们使用了lstCustomers.ItemsSource = e.Result;第一种绑定集合的属性,这时候就需要第二种绑定集合的属性 DisplayMemberPath="CustomerName"登场了。
其实实现上面的效果,你可以有三条途径:
1)设置控件的DisplayMemberPath属性为一个对象的属性名称,例如:DisplayMemberPath="CustomerName",效果如下图
2)重写对象的ToString()方法,提供一些有用的信息,设置可以显示几个属性,因为默认绑定使用的就是对象的tostring方法
{
return string .Format ("用户代码:{0} | 用户姓名:{1}",CustomerCode,CustomerName );
}
属性就不用修改了,还是<ListBox x:Name="lstCustomers" ></ListBox>,效果如下图
3)提供一个数据模板,这样你可以显示任何排列好的对象属性值,后面将会讲到这种做法。
到这里你可能又需要两外一个功能了,就是点击一个list中的item,在下面显示一下详细信息。你可以响应listbox的SelectionChanged事件,在事件代码中写上
{
gridCustomerDetails.DataContext = lstCustomers.SelectedItem;
}
前台设置为
发表评论
-
NET 应用架构指导 V2 学习笔记(十六) 服务层设计指导
2010-06-04 00:13 524如果你的应用是通 ... -
NET 应用架构指导 V2 学习笔记(十七) 组件设计指导
2010-06-05 00:48 625组件提供了一种将 ... -
NET 应用架构指导 V2 学习笔记(十八) 表现层组件设计指导
2010-06-05 21:09 490本章讲述的是你在设计用户界面组件和表现层逻辑组件的时候应该 ... -
NET 应用架构指导 V2 学习笔记(十九) 表现层组件设计指导
2010-06-06 06:15 5545 决定数据绑定的 ... -
NET 应用架构指导 V2 学习笔记(二十) 业务组件设计指导
2010-06-07 06:58 578前言 业务组件 ... -
微软企业库5.0学习笔记(四十二)异常处理模块
2010-06-14 00:04 803企业库的异常处理 ... -
关于程序员在30岁、35岁之后怎么办的新思考
2010-06-14 10:40 593首先给大家问个好 ... -
NET 应用架构指导 V2 学习笔记(二十四) 跨层关注问题
2010-06-17 20:00 549概况 大部分的 ... -
微软企业库5.0学习笔记(四十三)数据验证模块
2010-06-19 08:07 961概况 任何接受用户或者是其他系统输入的应用,一定要确保 ... -
关于项目进度慢的思考----如何提高整体开发效率
2010-06-21 23:42 770我们都是软件行业 ... -
微软企业库5.0学习笔记(四十四)实战数据验证模块
2010-06-23 19:22 7941 在业务对象上添加验证 添加对程序集【Microso ... -
微软企业库5.0学习笔记(四十五)实战数据验证模块----高级篇
2010-06-24 19:41 9421、添加自定义的提示信息 验证失败的提示信息可以自定义 ... -
面向对象类设计的五大原则(一)单一职责原则Single Responsibility Principle
2010-06-29 15:45 735引言 面向对象类设计,或者说是面向对象设计,有五大原则 ... -
《深入浅出设计模式-中文版》读书笔记 开篇乱弹(一)
2010-07-01 06:42 620oreilly的《Head.First ... -
《深入浅出设计模式-中文版》读书笔记-继承与组合(三)
2010-07-03 16:53 576经过上一次的改造 ... -
《深入浅出设计模式-中文版》读书笔记-观察者模式(四)
2010-07-06 06:34 600今天要接触的是观 ... -
利用attribute实现简单的ORM
2010-07-09 15:27 644我不知道NH的ORM具 ... -
系统内部模块(子系统)之间的耦合以及模块(子系统)划分
2010-07-14 13:02 788题外话 最近已经在努力学习了,学习基本功,学习设计模式 ... -
《深入浅出设计模式-中文版》读书笔记-工厂模式(五)
2010-07-16 12:46 668今天给大家带来的是:工厂模式。 我们在代码中创建一个对 ... -
Head.First.Object-Oriented.Design.and.Analysis《深入浅出面向对象的分析与设计》读书笔记(一)
2010-07-18 21:47 636题外话 又是一本Head.First系列的书,这个系列 ...
相关推荐
Android数据绑定的一个Demo程序,包括数据绑定和事件绑定
以下是Android Architecture Components的优势: ...Android Architecture Components中的Data Binding库可以帮助开发者将数据绑定到UI控件上,以减少手动设置UI控件的代码量,提高开发效率。 5. 易于学习和使用
Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET By Brian Noyes ............................................... Publisher: Addison Wesley ...
Android Data Binding结合RecyclerView的使用
Binding.zip 数据绑定案例
Binding.scala 是 Scala 上的数据绑定框架,既可以运行在 JVM 上,也可以通过 Scala.js 编译成 JavaScript 运行在 Node.js 或者浏览器中。Binding.scala 也是一套动态页面开发框架,使你可以直接在代码中内嵌 ...
Data Binding Validator by Ilhasoft The Data Binding Validator makes it easy and quick to validate fields in forms using data binding framework. Download Step 1: Add it in your root build.gradle at ...
Binding 数据绑定
This book is all about the what and the why of binding to data sources in a Windows Forms application built using Visual Studio 2005. The book goes into great detail in explaining the rationale behind...
本文主要介绍Android Data Binding数据绑定的知识,这里整理了详细的资料及简单示例代码帮助大家学习理解此部分知识,有需要的小伙伴可以参考下
Part 3: WPF Data Binding with LINQ to SQL These tutorials describe how to map your classes to your tables manually (rather than with an automated tool like SqlMetal) so that you can have support for M...
WPF Binding绑定 对象和view的绑定 界面修改直接改变对象的修改 Command命令 事件的绑定 基础学习源码
Brian Noyes is a software architect, trainer, writer, and speaker with IDesign (www.idesign.net), a premier .NET architecture and design consulting and training company. He has been developing ...
豆瓣电影搜索 利用Data Binding在RecycleView展示电影列表
DataGridView的常见操作源码,包含数据源绑定,添加删除修改选中单元格删除修改等等
nanoplex 数据绑定 #expressions 将变量、函数和对象绑定到属性和内部元素 用法 attr="{{var}}" {{var}} {{func(var)}} {{object.path.string}} 例子 {{function(var, var1)}} {{object.string}} #events 将函数...
Data Binding with Windows Forms 2.0 Programming Smart Client Data Applications with .NET
微软的示例! VB.net的 数据绑定!
数据绑定 统一的简单数据绑定。 用法 将包文件DataBind.unitypackage导入Unity 将DataBindContext添加到父对象 将Bind*组件添加到子级 使用dataBindContext[key] = value绑定值 查看Demo / DemoScene了解详细信息。...
Android Data Binding Adapter for ListView and RecyclerView..zip,使用新的android数据绑定框架将集合绑定到listviews和recyclerviews的简单方法