自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Silverlight數(shù)據(jù)綁定實(shí)現(xiàn)用戶信息

開發(fā) 開發(fā)工具
Silverlight數(shù)據(jù)綁定如何才能正確的操作來使它能幫助我們實(shí)現(xiàn)各種功能的需求呢?我們可以通過不斷的經(jīng)驗(yàn)積累來達(dá)到一種運(yùn)用靈活的程度。

Silverlight數(shù)據(jù)綁定的應(yīng)用,在實(shí)際編程中是一個(gè)非常重要的操作步驟。對于初學(xué)者來說,在剛剛學(xué)習(xí)的過程中一定要牢固掌握好這方面的知識點(diǎn),方便以后的應(yīng)用。#t#

在本示例中我們將做一個(gè)簡單的Silverlight數(shù)據(jù)綁定,用來顯示用戶信息,XAML如下:

  1. < Grid x:Name="LayoutRoot" 
    Background="#46461F"> 
  2. < Grid.RowDefinitions> 
  3. < RowDefinition Height="160">
  4. < /RowDefinition> 
  5. < RowDefinition Height="40">
  6. < /RowDefinition> 
  7. < RowDefinition Height="40">
  8. < /RowDefinition> 
  9. < /Grid.RowDefinitions> 
  10. < Grid.ColumnDefinitions> 
  11. < ColumnDefinition Width="150">
  12. < /ColumnDefinition> 
  13. < ColumnDefinition Width="*">
  14. < /ColumnDefinition> 
  15. < /Grid.ColumnDefinitions> 
  16. < Image Source="terrylee.jpg" 
    Width="78" Height="100" 
  17. HorizontalAlignment="Left" 
    Grid.Row="0" Grid.Column="1"/> 
  18. < TextBlock Foreground="White" 
    FontSize="18" Text="姓名:" 
  19. Grid.Row="1" Grid.Column="0" 
    HorizontalAlignment="Right"/> 
  20. < TextBlock x:Name="lblName" 
    Foreground="White" FontSize="18" 
  21. Grid.Row="1" Grid.Column="1" 
    HorizontalAlignment="Left"/> 
  22. < TextBlock Foreground="White"
     FontSize="18" Text="位置:" 
  23. Grid.Row="2" Grid.Column="0" 
    HorizontalAlignment="Right"/> 
  24. < TextBlock x:Name="lblAddress" 
    Foreground="White" FontSize="18" 
  25. Grid.Row="2" Grid.Column="1" 
    HorizontalAlignment="Left"/> 
  26. < /Grid> 

添加一個(gè)簡單User類,它具有Name和Address兩個(gè)屬性:

 

  1. public class User  
  2. {  
  3. public string Name 
    { get; set; }  
  4. public string Address 
    { get; set; }  

使用Silverlight數(shù)據(jù)綁定句法{Binding Property}進(jìn)行數(shù)據(jù)綁定,注意下面的兩個(gè)TextBlock控件Text屬性:

  1. < Grid x:Name="LayoutRoot"
     Background="#46461F"> 
  2. < Grid.RowDefinitions> 
  3. < RowDefinition Height="160">
  4. < /RowDefinition> 
  5. < RowDefinition Height="40">
  6. < /RowDefinition> 
  7. < RowDefinition Height="40">
  8. < /RowDefinition> 
  9. < /Grid.RowDefinitions> 
  10. < Grid.ColumnDefinitions> 
  11. < ColumnDefinition Width="150">
  12. < /ColumnDefinition> 
  13. < ColumnDefinition Width="*">
  14. < /ColumnDefinition> 
  15. < /Grid.ColumnDefinitions> 
  16. < Image Source="terrylee.jpg" 
    Width="78" Height="100" 
  17. HorizontalAlignment="Left" Grid.
    Row
    ="0" Grid.Column="1"/> 
  18. < TextBlock Foreground="White" 
    FontSize="18" Text="姓名:" 
  19. Grid.Row="1" Grid.Column="0" 
    HorizontalAlignment="Right"/> 
  20. < TextBlock x:Name="lblName" 
    Foreground="White" FontSize="18" 
  21. Grid.Row="1" Grid.Column="1" 
    HorizontalAlignment="Left" 
  22. Text="{Binding Name}"/> 
  23. < TextBlock Foreground="White" 
    FontSize="18" Text="位置:" 
  24. Grid.Row="2" Grid.Column="0" 
    HorizontalAlignment="Right"/> 
  25. < TextBlock x:Name="lblAddress" 
    Foreground="White" FontSize="18" 
  26. Grid.Row="2" Grid.Column="1" 
    HorizontalAlignment="Left" 
  27. Text="{Binding Address}"/> 
  28. < /Grid> 

指定數(shù)據(jù)源,注意這里是創(chuàng)建一個(gè)User的實(shí)例并賦值后,把user實(shí)例綁定到了TextBlock的DataContext上,而不是向之前我們所做的示例中那樣,直接指定Text屬性:

 

  1. private void UserControl_Loaded
    (object sender, RoutedEventArgs e)  
  2. {  
  3. User user = new User();  
  4. user.Name = "TerryLee";  
  5. user.Address = "中國 天津";  
  6. lblName.DataContext = user;  
  7. lblAddress.DataContext = user;  

上面這種Silverlight數(shù)據(jù)綁定模式,只是顯示數(shù)據(jù)而不對數(shù)據(jù)做任何修改,默認(rèn)的綁定模式是一次綁定OneTime。

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2009-12-30 09:38:37

Silverlight

2016-10-24 23:18:55

數(shù)據(jù)分析漏斗留存率

2010-04-23 13:23:42

Silverlight

2009-12-23 10:46:38

WPF實(shí)現(xiàn)用戶界面

2009-12-30 13:51:43

Silverlight

2024-09-22 10:46:33

數(shù)據(jù)飛輪算法

2012-05-04 09:28:49

Linux

2010-01-28 10:00:54

linux用戶注銷logout

2014-07-22 14:48:05

2010-08-04 10:48:17

路由器

2009-12-30 14:10:27

Silverlight

2018-04-02 10:16:00

bug代碼安卓

2009-12-30 10:15:57

Silverlight

2016-05-17 10:03:39

用戶體驗(yàn)運(yùn)維可度量

2025-03-05 07:58:30

2018-05-30 10:22:47

電商平臺(tái)

2019-08-22 15:42:03

2025-03-28 04:10:00

2010-08-05 15:06:19

Flex數(shù)據(jù)綁定

2009-12-30 16:19:49

Silverlight
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號