- 浏览: 475732 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (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)
今天我们讨论的是Silverlight3中的数据绑定,内容来自 《Pro Silverlight3 in C#》的读后感,中文名称可以译为《Silverlight3高级编程 C#版》。我找到的是一本PDF的,在网上可以搜索到下载地址。
数据绑定提供了一种,从对象中获取信息,然后显示在你的应用程序的界面上,同时不需要写冗长的代码就可以完成所有的工作的方式。通常情况下,富客户端提供两种绑定方式,不仅可以从兑现获取数据,显示到界面上,也可以将界面的数据传回给对象。
Silverlight是不允许客户端直接使用ADO.NET直接连接数据库,进行数据库操作的。只能通过代理的方式操作数据库,通过第三方,例如:webservice、wcf、ado.net data service、ric service等服务性的方式。
1、绑定到数据对象
例如现在又一个Customer的实体对象
public class Customer
{
private int _intCustomerId;
private string _strCustomerName;
private string _strCustomerCode;
[DataMember]
public virtual int CustomerId
{
get { return this._intCustomerId; }
set { this._intCustomerId = value; }
}
[DataMember]
public virtual string CustomerName
{
get { return this._strCustomerName; }
set { this._strCustomerName = value; }
}
[DataMember]
public virtual string CustomerCode
{
get { return _strCustomerCode; }
set { this._strCustomerCode = value; }
}
}
想要达到下图的一种效果
<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>
后台代码
{
Customer customer = new Customer() { CustomerId = 1, CustomerCode = "ss", CustomerName = "dddd" };
LayoutRoot.DataContext = customer;
}
2、将数据对象存储为资源
首先要在类代码中存在一个资源对象,
{
public class Users
{
public string Firstname { get; set; }
public string Lastname { get; set; }
}
public partial class DataBindingDemo : UserControl
{
public DataBindingDemo()
{
InitializeComponent();
}
}
}
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Silverlight"
Width="400" Height="300">
<UserControl.Resources>
<local:Users x:Key="SingleUser" Firstname="virus" Lastname="ssss"></local:Users>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<TextBox Text="{Binding Path=Firstname,Source={StaticResource SingleUser}}"></TextBox>
</Grid>
</UserControl>
上面的红色部分的xmlns:local="clr-namespace:Silverlight"是定义资源的命名空间,
<UserControl.Resources>
<local:Users x:Key="SingleUser" Firstname="virus" Lastname="ssss"></local:Users>
</UserControl.Resources>
是定义一个资源
<TextBox Text="{Binding Path=Firstname,Source={StaticResource SingleUser}}"></TextBox>
是绑定到控件,源是SingleUser,绑定的具体属性是Firstname。如果是绑定一个属性的话,每次都需要设置Source的值,如果是绑定到一个Grid上面的话,可以使用<Grid Name="gridUsers" DataContext="{StaticResource SingleUser}">来绑定。
3、可编辑的双向绑定
到这里你可能想要知道,如果我在界面上修改一个值,会发生什么呢?例如你修改了界面上面的文本框的值,文本框的值绑定了一个对象的属性,在内存中的对象属性会改变吗?
实际上,内存中的对象属性没有任何变化,因为Silverlight默认使用单向绑定。
System.Windows.Data.BindingMode枚举量中定义了全部的绑定类型,如下表:
Name |
Description |
OneWay 单向绑定 |
当对象属性改变的时候,绑定对象就会改变 |
TwoWay 双向绑定 |
当源对象属性改变的时候,绑定对象会改变;反过来,绑定对象的改变也会影响源对象属性的改变。 |
OneTime 一次性绑定 |
绑定对象在第一次初始化的时候显示对象的属性,后面不会随着对象属性的改变而改变,这种适用于你确定对象的属性几乎不会改变的情况下。 |
<TextBox Text="{Binding Firstname, Mode=TwoWay}"></TextBox>
如果你使用了双向绑定,如果你修改了textbox的值,当你的鼠标焦点一离开textbox之后(焦点在两外一个控件,或者点击一个按钮),内存中的对象属性Firstname就会发生改变,执行属性的set。
使用了双向绑定之后,焦点一离开控件之后就会触发内存对象的改变,但是有的时候我们想精确控制,而不是等焦点移除的时候,例如:输入框在用户输入的同时就进行,而不是等用户焦点移出输入框,你可以在代码中调用 BindingExpression.UpdateSource();
{
BindingExpression expression = txtFirstname.GetBindingExpression(TextBox.TextProperty);
expression.UpdateSource();
}
使用上面的代码,你就可以在用户进入文本框和输入的时候就强迫文本框更新源对象的属性值。如果你全部的控件都是用这样的更新方式,你就可以禁用Silverlight的自动更新功能,设置UpdateSourceTrigger属性的值为Explicit。
Text="{Binding Path=Firstname,Source={StaticResource SingleUser}, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
TextChanged="txtFirstname_TextChanged"></TextBox>
在Silverlight中UpdateSourceTrigger有两个属性值可以设置:Default和Explicit。
4、数据验证 Validation
数据验证将在下一篇中重点讲解。先提供一个实例代码下载:DataValidation
验证的话,就需要Silverlight客户端和wcf服务端共享类库,共享实体类,因为服务端的实体类虽然被序列化到客户端,但是只能序列化状态信息,就是成员,方法之类的不会序列化的,而且就算序列化过来,也没有什么用,运行环境context都不一样了,没有什么意思。所以可能会用到客户端和服务端共享类库,可以参考下面的文章。
Silverlight客户端和WCF服务器端共享类库
5、绑定到一个数据服务
更复杂的数据绑定中,数据可能是从外部服务获取的。这里的外部服务使用wcf来提供,下面的代码假定你已经了解wcf,如果不了解的可以看老徐的博客或者google一些资料来看。数据库操作部分使用NHibernate 2.1.2完成。
首先定义数据交互契约
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace Domain.Server
{
[DataContract]
public class Customer : INotifyPropertyChanged
{
private int _intCustomerId;
private string _strCustomerName;
private string _strCustomerCode;
[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.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.Server;
using Common.Core;
using Common.Data;
namespace WcfService
{
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());
//string name = OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
}
// Add more operations here and mark them with [OperationContract]
public Domain.Server.Customer GetCustomer(int customerId)
{
Domain.Server.Customer objCustomer = new Domain.Server.Customer();
return _customerDao.GetCustomerById(customerId);
}
}
}
Silverlight的前台代码
<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>
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;
namespace Silverlight
{
public partial class sldbdemo : UserControl
{
ServiceCustomerClient client;
SysUser _sysUser;
public sldbdemo()
{
InitializeComponent();
client = new ServiceCustomerClient();
GetCustomerById(1);
}
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)
{
LayoutRoot.DataContext = e.Result;
}
}
}
代码无误,就应该看到下面的内容,前提是数据库有Id=1的用户信息。数据操作部分也可以自己写DbConnection来做,效果是一样的。
6、绑定一个数据集合
下文继续。
代码可以在http://silverlightwcf.codeplex.com/下载。
发表评论
-
NET 应用架构指导 V2 学习笔记(十六) 服务层设计指导
2010-06-04 00:13 527如果你的应用是通 ... -
NET 应用架构指导 V2 学习笔记(十七) 组件设计指导
2010-06-05 00:48 628组件提供了一种将 ... -
NET 应用架构指导 V2 学习笔记(十八) 表现层组件设计指导
2010-06-05 21:09 498本章讲述的是你在设计用户界面组件和表现层逻辑组件的时候应该 ... -
NET 应用架构指导 V2 学习笔记(十九) 表现层组件设计指导
2010-06-06 06:15 5605 决定数据绑定的 ... -
NET 应用架构指导 V2 学习笔记(二十) 业务组件设计指导
2010-06-07 06:58 581前言 业务组件 ... -
微软企业库5.0学习笔记(四十二)异常处理模块
2010-06-14 00:04 808企业库的异常处理 ... -
关于程序员在30岁、35岁之后怎么办的新思考
2010-06-14 10:40 598首先给大家问个好 ... -
NET 应用架构指导 V2 学习笔记(二十四) 跨层关注问题
2010-06-17 20:00 553概况 大部分的 ... -
微软企业库5.0学习笔记(四十三)数据验证模块
2010-06-19 08:07 967概况 任何接受用户或者是其他系统输入的应用,一定要确保 ... -
关于项目进度慢的思考----如何提高整体开发效率
2010-06-21 23:42 774我们都是软件行业 ... -
微软企业库5.0学习笔记(四十四)实战数据验证模块
2010-06-23 19:22 7971 在业务对象上添加验证 添加对程序集【Microso ... -
微软企业库5.0学习笔记(四十五)实战数据验证模块----高级篇
2010-06-24 19:41 9511、添加自定义的提示信息 验证失败的提示信息可以自定义 ... -
面向对象类设计的五大原则(一)单一职责原则Single Responsibility Principle
2010-06-29 15:45 739引言 面向对象类设计,或者说是面向对象设计,有五大原则 ... -
《深入浅出设计模式-中文版》读书笔记 开篇乱弹(一)
2010-07-01 06:42 624oreilly的《Head.First ... -
《深入浅出设计模式-中文版》读书笔记-继承与组合(三)
2010-07-03 16:53 581经过上一次的改造 ... -
《深入浅出设计模式-中文版》读书笔记-观察者模式(四)
2010-07-06 06:34 605今天要接触的是观 ... -
利用attribute实现简单的ORM
2010-07-09 15:27 649我不知道NH的ORM具 ... -
系统内部模块(子系统)之间的耦合以及模块(子系统)划分
2010-07-14 13:02 790题外话 最近已经在努力学习了,学习基本功,学习设计模式 ... -
《深入浅出设计模式-中文版》读书笔记-工厂模式(五)
2010-07-16 12:46 671今天给大家带来的是:工厂模式。 我们在代码中创建一个对 ... -
Head.First.Object-Oriented.Design.and.Analysis《深入浅出面向对象的分析与设计》读书笔记(一)
2010-07-18 21:47 640题外话 又是一本Head.First系列的书,这个系列 ...
相关推荐
Android数据绑定的一个Demo程序,包括数据绑定和事件绑定
Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET By Brian Noyes ............................................... Publisher: Addison Wesley ...
以下是Android Architecture Components的优势: ...Android Architecture Components中的Data Binding库可以帮助开发者将数据绑定到UI控件上,以减少手动设置UI控件的代码量,提高开发效率。 5. 易于学习和使用
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展示电影列表
nanoplex 数据绑定 #expressions 将变量、函数和对象绑定到属性和内部元素 用法 attr="{{var}}" {{var}} {{func(var)}} {{object.path.string}} 例子 {{function(var, var1)}} {{object.string}} #events 将函数...
DataGridView的常见操作源码,包含数据源绑定,添加删除修改选中单元格删除修改等等
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的简单方法