Silverlight數(shù)據(jù)綁定實(shí)現(xiàn)用戶信息
Silverlight數(shù)據(jù)綁定的應(yīng)用,在實(shí)際編程中是一個(gè)非常重要的操作步驟。對于初學(xué)者來說,在剛剛學(xué)習(xí)的過程中一定要牢固掌握好這方面的知識點(diǎn),方便以后的應(yīng)用。#t#
在本示例中我們將做一個(gè)簡單的Silverlight數(shù)據(jù)綁定,用來顯示用戶信息,XAML如下:
- < Grid x:Name="LayoutRoot"
Background="#46461F"> - < Grid.RowDefinitions>
- < RowDefinition Height="160">
- < /RowDefinition>
- < RowDefinition Height="40">
- < /RowDefinition>
- < RowDefinition Height="40">
- < /RowDefinition>
- < /Grid.RowDefinitions>
- < Grid.ColumnDefinitions>
- < ColumnDefinition Width="150">
- < /ColumnDefinition>
- < ColumnDefinition Width="*">
- < /ColumnDefinition>
- < /Grid.ColumnDefinitions>
- < Image Source="terrylee.jpg"
Width="78" Height="100" - HorizontalAlignment="Left"
Grid.Row="0" Grid.Column="1"/> - < TextBlock Foreground="White"
FontSize="18" Text="姓名:" - Grid.Row="1" Grid.Column="0"
HorizontalAlignment="Right"/> - < TextBlock x:Name="lblName"
Foreground="White" FontSize="18" - Grid.Row="1" Grid.Column="1"
HorizontalAlignment="Left"/> - < TextBlock Foreground="White"
FontSize="18" Text="位置:" - Grid.Row="2" Grid.Column="0"
HorizontalAlignment="Right"/> - < TextBlock x:Name="lblAddress"
Foreground="White" FontSize="18" - Grid.Row="2" Grid.Column="1"
HorizontalAlignment="Left"/> - < /Grid>
添加一個(gè)簡單User類,它具有Name和Address兩個(gè)屬性:
- public class User
- {
- public string Name
{ get; set; }- public string Address
{ get; set; }- }
使用Silverlight數(shù)據(jù)綁定句法{Binding Property}進(jìn)行數(shù)據(jù)綁定,注意下面的兩個(gè)TextBlock控件Text屬性:
- < Grid x:Name="LayoutRoot"
Background="#46461F">- < Grid.RowDefinitions>
- < RowDefinition Height="160">
- < /RowDefinition>
- < RowDefinition Height="40">
- < /RowDefinition>
- < RowDefinition Height="40">
- < /RowDefinition>
- < /Grid.RowDefinitions>
- < Grid.ColumnDefinitions>
- < ColumnDefinition Width="150">
- < /ColumnDefinition>
- < ColumnDefinition Width="*">
- < /ColumnDefinition>
- < /Grid.ColumnDefinitions>
- < Image Source="terrylee.jpg"
Width="78" Height="100"- HorizontalAlignment="Left" Grid.
Row="0" Grid.Column="1"/>- < TextBlock Foreground="White"
FontSize="18" Text="姓名:"- Grid.Row="1" Grid.Column="0"
HorizontalAlignment="Right"/>- < TextBlock x:Name="lblName"
Foreground="White" FontSize="18"- Grid.Row="1" Grid.Column="1"
HorizontalAlignment="Left"- Text="{Binding Name}"/>
- < TextBlock Foreground="White"
FontSize="18" Text="位置:"- Grid.Row="2" Grid.Column="0"
HorizontalAlignment="Right"/>- < TextBlock x:Name="lblAddress"
Foreground="White" FontSize="18"- Grid.Row="2" Grid.Column="1"
HorizontalAlignment="Left"- Text="{Binding Address}"/>
- < /Grid>
指定數(shù)據(jù)源,注意這里是創(chuàng)建一個(gè)User的實(shí)例并賦值后,把user實(shí)例綁定到了TextBlock的DataContext上,而不是向之前我們所做的示例中那樣,直接指定Text屬性:
- private void UserControl_Loaded
(object sender, RoutedEventArgs e)- {
- User user = new User();
- user.Name = "TerryLee";
- user.Address = "中國 天津";
- lblName.DataContext = user;
- lblAddress.DataContext = user;
- }
上面這種Silverlight數(shù)據(jù)綁定模式,只是顯示數(shù)據(jù)而不對數(shù)據(jù)做任何修改,默認(rèn)的綁定模式是一次綁定OneTime。