.NEt专家博客!

【李会军】一步一步学Silverlight 2系列(11):数据绑定

上一篇 / 下一篇  2008-04-11 19:36:22

概念

Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章带您快速进入Silverlight 2开发。

本文为系列文章第十一篇,主要介绍Silverlight 2中的数据绑定。

数据绑定模式

在Silverlight 2中,支持三种模式的数据绑定。

1.OneTime:一次绑定,在绑定创建时使用源数据更新目标,适用于只显示数据而不进行数据的更新。

2.OneWay:单向绑定,在绑定创建时或者源数据发生变化时更新到目标,适用于显示变化的数据。

3.TwoWay:双向绑定,在任何时候都可以同时更新源数据和目标。

Jesse Liberty举 的例子非常的形象,使用Silverlight开发一个在线书店,显示书籍的书名、作者等信息,使用OneTime模式,这些数据一般不会发生变化的;显 示价格信息时使用OneWay模式,因为管理员可能会在一天内调整价格;显示书籍的剩余数量时用TwoWay模式,数量随着用户的订购会随时发生变化,即 目标和源数据都要进行更新。

简单数据绑定

在本示例中我们将做一个简单的数据绑定,用来显示用户信息,XAML如下:

<Gridx:Name="LayoutRoot"Background="#46461F">ITPUB个人空间!b1}U
d#kRed
<
Grid.RowDefinitions>ITPUB个人空间D6QJ"u#u:KyF
<
RowDefinitionHeight="160"></RowDefinition>ITPUB个人空间,xEV?aPY[
<
RowDefinitionHeight="40"></RowDefinition>ITPUB个人空间y;p t*t T Lrc8j
<
RowDefinitionHeight="40"></RowDefinition>
J^,_Uw-[ x0</
Grid.RowDefinitions>
+Z4\0?(g.o4i#H DI0<
Grid.ColumnDefinitions>
@gX7Pba/?&J9A.e0<
ColumnDefinitionWidth="150"></ColumnDefinition>
X!E#E}4M%Y2Bx0<
ColumnDefinitionWidth="*"></ColumnDefinition>ITPUB个人空间'CNtz@X/|7V_6E
</
Grid.ColumnDefinitions>
}.h5g"iBD s0<
ImageSource="terrylee.jpg"Width="78"Height="100"ITPUB个人空间&nD&Tb_|P ZN
HorizontalAlignment="Left"Grid.Row="0"Grid.Column="1"/>ITPUB个人空间8d{!U.SB
<
TextBlockForeground="White"FontSize="18"Text="姓名:"
,V|to9Uj4P0
Grid.Row="1"Grid.Column="0"HorizontalAlignment="Right"/>ITPUB个人空间/F,KXu2x5L*S
<
TextBlockx:Name="lblName"Foreground="White"FontSize="18"ITPUB个人空间[H5[e;S
Grid.Row="1"Grid.Column="1"HorizontalAlignment="Left"/>ITPUB个人空间"?.x?)c VD
<
TextBlockForeground="White"FontSize="18"Text="位置:"ITPUB个人空间g$@w] ^ z0b ^n
Grid.Row="2"Grid.Column="0"HorizontalAlignment="Right"/>
(pn)k,V"IVM0<
TextBlockx:Name="lblAddress"Foreground="White"FontSize="18"ITPUB个人空间tQd GUB;d6sr
Grid.Row="2"Grid.Column="1"HorizontalAlignment="Left"/>
2z5_$n#t%R-^q:Ea'{0</
Grid>

添加一个简单User类,它具有Name和Address两个属性:

public classUser
N7Ve v)HBd,M&m0
{
%?-@"dv3o S0public stringName {get;set; }ITPUB个人空间D#fyoD'h z)tdZ
ITPUB个人空间,],e"k |d5~
public stringAddress {get;set; }
[,|o8C;O5U x0}

使用绑定句法{Binding Property}进行数据绑定,注意下面的两个TextBlock控件Text属性:

<Gridx:Name="LayoutRoot"Background="#46461F">ITPUB个人空间8o$E2H0|c4K(r,Z
<
Grid.RowDefinitions>
:}#bo-]E fz'ra y Y0<
RowDefinitionHeight="160"></RowDefinition>ITPUB个人空间'W*DJ7LF P V)_
<
RowDefinitionHeight="40"></RowDefinition>ITPUB个人空间)}l8tv(F%en)fe+U
<
RowDefinitionHeight="40"></RowDefinition>ITPUB个人空间 E(v7|*ctW
</
Grid.RowDefinitions>
z V,v a+B0<
Grid.ColumnDefinitions>ITPUB个人空间8dN!f!|~2~
<
ColumnDefinitionWidth="150"></ColumnDefinition>
C&BQ UJ2G b%Q0<
ColumnDefinitionWidth="*"></ColumnDefinition>ITPUB个人空间+l'Y6Xglu
</
Grid.ColumnDefinitions>ITPUB个人空间!M/Z R+D1R(FD
<
ImageSource="terrylee.jpg"Width="78"Height="100"ITPUB个人空间 `Q3t0^E A%F/S5H
HorizontalAlignment="Left"Grid.Row="0"Grid.Column="1"/>ITPUB个人空间g3~?__
<
TextBlockForeground="White"FontSize="18"Text="姓名:"ITPUB个人空间s]a0pE)Zg
Grid.Row="1"Grid.Column="0"HorizontalAlignment="Right"/>
UF([;N!yG0<
TextBlockx:Name="lblName"Foreground="White"FontSize="18"ITPUB个人空间)Mtb Mj?(R
Grid.Row="1"Grid.Column="1"HorizontalAlignment="Left"ITPUB个人空间3]-w _ S.v4LH@
Text="{BindingName}"/>
-v8p-y l8a8l#b2rY0<
TextBlockForeground="White"FontSize="18"Text="位置:"ITPUB个人空间S0\r9u5~#kX3X
Grid.Row="2"Grid.Column="0"HorizontalAlignment="Right"/>ITPUB个人空间"[#Uy*sx9a
<
TextBlockx:Name="lblAddress"Foreground="White"FontSize="18"
H@Y6a,klm0
Grid.Row="2"Grid.Column="1"HorizontalAlignment="Left"
(b@4s|,N;K6N0
Text="{BindingAddress}"/>
F&fyvH J!O0</
Grid>

指定数据源,注意这里是创建一个User的实例并赋值后,把user实例绑定到了TextBlock的DataContext上,而不是向之前我们所做的示例中那样,直接指定Text属性:

private voidUserControl_Loaded(objectsender,RoutedEventArgse)ITPUB个人空间HNfn:NP6O(fO'F1@
{
6Cv1_/Xk:d.Jf-j,?0Useruser =newUser();ITPUB个人空间/x `[u"G
user.Name ="TerryLee";
+djd+[ ^0M9HJ;z0user.Address ="中国 天津";
0@/V8iFvvN0ITPUB个人空间6s&c;WW1t,C
lblName.DataContext = user;ITPUB个人空间}P&d0z#t9Dd @
lblAddress.DataContext = user;
Dv ?-t G [0}

运行示例后,可以看到:ITPUB个人空间(a~DOo U-mn
ITPUB个人空间7?or/q+K

上面这种数据绑定模式,只是显示数据而不对数据做任何修改,默认的绑定模式是一次绑定OneTime。

单向绑定示例

如果需要在数据源发生变化时能够通知UI进行相应的更新,即使用单向绑定OneWay或者双向绑定TwoWay,则业务实体需要实现接口 INotifyPropertyChanged。在本示例中,我们加上一个更新按钮,当单击按钮时更新user实例的属性值,会看到界面上的数据也会发生 变化。

修改一下User类,使其实现INotifyPropertyChanged接口。

public classUser:INotifyPropertyChanged
GLjMl0
{
,e1d.d:P w0public eventPropertyChangedEventHandlerPropertyChanged;ITPUB个人空间4t c ni bu`$^
ITPUB个人空间+w0W:z"W'i8D x
private string_name;
XPO/b3U1PLi0public stringName
P_7MZNBN7@0{ITPUB个人空间1P i;kw|!y\
get{return_name; }ITPUB个人空间l `\5Q5zDn.Rkb
set
"D~2efjv0
{
T$HY&^5s1c#e6bo2h0_name =value;
i^%xwr'YJ `6g0if(PropertyChanged !=null)ITPUB个人空间%J*o0C!OY+W|+f$N
{ITPUB个人空间l;Z@0u P
PropertyChanged(this,newPropertyChangedEventArgs("Name"));ITPUB个人空间Cb A0lrr
}ITPUB个人空间2c0Lm8]? xU/p1t4R
}
}Up\D f0}ITPUB个人空间6cI|V3h5B-iD.R

'HJ QA_3o0private string_address;
)^|2rb.?[B0public stringAddress
Bdg7Q&[zC0{ITPUB个人空间2i,L\a ?J4~u
get{return_address; }ITPUB个人空间%m |]S"m@c"xW0w
set
KY5^ qb-Jw6Fi0
{ITPUB个人空间 o*HAW-]p+u
_address =value;
,F0B&mThc ~0if(PropertyChanged !=null)ITPUB个人空间 {T4u4Pb8c wl
{
DN:af+]@0PropertyChanged(this,newPropertyChangedEventArgs("Address"));ITPUB个人空间Y0d)K1x0aHv:t
}
8C;]6kP}k0]0}ITPUB个人空间j'\l&X1X\Va
}
f s^8}(tI!BH+j0}

修改数据绑定模式,使用单向绑定OneWay模式,如{Binding Address, Mode=OneWay}

<Gridx:Name="LayoutRoot"Background="#46461F">
7P7`5e!`E]3np0<
Grid.RowDefinitions>
7t}z%|+grm0<
RowDefinitionHeight="160"></RowDefinition>
"iB:Ft u&{{0<
RowDefinitionHeight="40"></RowDefinition>ITPUB个人空间7V [w([8o Y+t
<
RowDefinitionHeight="40"></RowDefinition>
;x ^,U"iZWj1ca,O0</
Grid.RowDefinitions>ITPUB个人空间!j4lF] V6o#F
<
Grid.ColumnDefinitions>ITPUB个人空间`#s`dW1w%e5B
<
ColumnDefinitionWidth="150"></ColumnDefinition>
0L];D"}YNu!g0<
ColumnDefinitionWidth="*"></ColumnDefinition>ITPUB个人空间+V;hwYIs,k
</
Grid.ColumnDefinitions>
`l)},A%y)A(t(}0<
ImageSource="terrylee.jpg"Width="78"Height="100"
[6HNS#^L6r0
HorizontalAlignment="Left"Grid.Row="0"Grid.Column="1"/>
\?V7Y#t:EH9i0<
Buttonx:Name="btnUpdate"Width="100"Height="40"ITPUB个人空间Bd)|Cu-[;l
Content="Update"Click="btnUpdate_Click"/>ITPUB个人空间-jX3z"xD;Q}#E'v
<
TextBlockForeground="White"FontSize="18"Text="姓名:"
TQ0c5q'YT/A0
Grid.Row="1"Grid.Column="0"HorizontalAlignment="Right"/>ITPUB个人空间~)W+[C8| v
<
TextBlockx:Name="lblName"Foreground="White"FontSize="18"
B*G?Y,EU0
Grid.Row="1"Grid.Column="1"HorizontalAlignment="Left"ITPUB个人空间],w)[{@k
Text="{BindingName,Mode=OneWay}"/>ITPUB个人空间!w]`R*Q0b
<
TextBlockForeground="White"FontSize="18"Text="位置:"
'OP-z|? U-h0
Grid.Row="2"Grid.Column="0"HorizontalAlignment="Right"/>ITPUB个人空间UP ~gk?;Ye
<
TextBlockx:Name="lblAddress"Foreground="White"FontSize="18"ITPUB个人空间x5{;q5^(Ya
Grid.Row="2"Grid.Column="1"HorizontalAlignment="Left"
J7hl&T/{7A E0
Text="{BindingAddress,Mode=OneWay}"/>ITPUB个人空间7KT9fQw F
</
Grid>

编写事件处理程序,为了演示把user声明为一个全局的,并在按钮的单击事件中修改其属性值:

public partial classPage:UserControl
(U.F]E\+Zx0
{ITPUB个人空间T7Vt+U$l8U
publicPage()
4Frcx~adj0{ITPUB个人空间 G|1qJ4S
InitializeComponent();
Y#_%}`w0}
pz7^ _ XC:p.D*x0Useruser;ITPUB个人空间$P?2xck ]F
private voidUserControl_Loaded(objectsender,RoutedEventArgse)
+l"\.RN.V0{
7@9U};_cbR_0user =newUser();
?N0fr)Gkua0user.Name ="TerryLee";
*W'L bw/J In5z(d0user.Address ="中国 天津";ITPUB个人空间_:P)^0hVNp%J
ITPUB个人空间qKvqP
lblName.DataContext = user;
BZpH"v/l0lblAddress.DataContext = user;ITPUB个人空间 R+p'd+zu:cq
}ITPUB个人空间%cL.f~2]7L

%ubx$Y'Qpq(\`0private voidbtnUpdate_Click(objectsender,RoutedEventArgse)
&e6YIe6Z0{
M,NJ/J bk0user.Name ="李会军";ITPUB个人空间#Gd;B3fo(H/qF
user.Address ="China Tianjin";ITPUB个人空间Z#Fhg_GOZ4`
}
bJ&_]Z0}

运行后如下所示:

单击Update按钮后:ITPUB个人空间},YODU U:@g%G q
ITPUB个人空间0T7jK4ggu*v+i?;_

绑定到列表

下面再看一个绑定到列表的简单例子,一般都会使用DataGrid或者ListBox来进行列表数据的显示。下面的示例我们显示一个文章列表:

<GridBackground="#46461F">
)D-z.a5ZO jW0<
Grid.RowDefinitions>ITPUB个人空间Wh6r7Pj0Rdx
<
RowDefinitionHeight="40"></RowDefinition>ITPUB个人空间-i'h^0ifyt
<
RowDefinitionHeight="*"></RowDefinition>
nf^TP0</
Grid.RowDefinitions>
YT5I0?(y"w:iu0<
Grid.ColumnDefinitions>ITPUB个人空间 ]V#mE3Mz.M]
<
ColumnDefinition></ColumnDefinition>ITPUB个人空间tI"uFM9U8G["A
</
Grid.ColumnDefinitions>
)pA,M"`1O} e$](wK`0<
BorderGrid.Row="0"Grid.Column="0"CornerRadius="15"ITPUB个人空间5LZk$xM(L\)s ]
Width="240"Height="36"Background="Orange"
~&vi(t&P ?C0
Margin="20 0 0 0"HorizontalAlignment="Left">
8I*e'Lw/V2k A g^|f4\0<
TextBlockText="文章列表"Foreground="White"ITPUB个人空间}/AXX#B["w'R$m
HorizontalAlignment="Left"VerticalAlignment="Center"
\ R LBBI-{'X*QU ?I0
Margin="20 0 0 0"></TextBlock>
6L0cq ?f%dl0</
Border>
$D0yZ0y6x/\%j$b0<
ListBoxx:Name="PostList"Grid.Column="0"Grid.Row="1"
w gTi?M0
Margin="40 10 10 10"
:tO(_%N#VKH#o8q0
HorizontalContentAlignment="Left"VerticalContentAlignment="Bottom"
"n ? Vt7?$D2Y3c$W-R3b0
ItemsSource="{BindingPosts}">
T-uh:Mpy e&p0</
ListBox>ITPUB个人空间MZ"?H(U
</
Grid>

编写一个简单的业务类:

public classBlog
b2x3zXr1g'u0
{
@"~hO*{0R0publicList<String> Posts {get;set; }ITPUB个人空间-s1a pC/` A5_*E
}

初始化集合数据并进行绑定

private voidUserControl_Loaded(objectsender,RoutedEventArgse)ITPUB个人空间Y}whNZF0el
{ITPUB个人空间$y5D U{$VL%C(U9d?:i
Blogblog =newBlog();ITPUB个人空间7X6ygu|a.?i5y
blog.Posts =newList<String>
'p'P&@ VlRF0{ITPUB个人空间)M9v.P'p1k,T([(W
"一步一步学Silverlight 2系列(10):使用用户控件",ITPUB个人空间"]6U{cE)N$Qy%c
"一步一步学Silverlight 2系列(9):使用控件模板",
2Pzce6N7vU$B4h0"一步一步学Silverlight 2系列(8):使用样式封装控件观感",ITPUB个人空间OOfw|z
"一步一步学Silverlight 2系列(7):全屏模式支持"
\m,f"gY?Z0
};
lNW2c,i.}{0
ZC!b r8E i9L(pr0PostList.DataContext = blog;ITPUB个人空间UFm}!j \(P
}

最终运行的结果如下所示:

当然我们也可以使用ListBox的ItemsSource属性进行绑定,

结束语

本文简单介绍了Silverlight 2中的数据绑定,你可以从这里下载文章示例代码。

作者:TerryLeeITPUB个人空间 V/Laz!_%a ]v%v|(S
出处:http://terrylee.cnblogs.com

TAG:

引用 删除 Guest   /   2010-02-22 16:52:55
-1
引用 删除 Guest   /   2009-09-09 16:56:19
1
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar