Silverlight数据绑定实现用户信息

开发 开发工具
Silverlight数据绑定如何才能正确的操作来使它能帮助我们实现各种功能的需求呢?我们可以通过不断的经验积累来达到一种运用灵活的程度。

Silverlight数据绑定的应用,在实际编程中是一个非常重要的操作步骤。对于初学者来说,在刚刚学习的过程中一定要牢固掌握好这方面的知识点,方便以后的应用。#t#

在本示例中我们将做一个简单的Silverlight数据绑定,用来显示用户信息,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> 

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

 

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

使用Silverlight数据绑定句法{Binding Property}进行数据绑定,注意下面的两个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> 

指定数据源,注意这里是创建一个User的实例并赋值后,把user实例绑定到了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数据绑定模式,只是显示数据而不对数据做任何修改,默认的绑定模式是一次绑定OneTime。

责任编辑:曹凯 来源: 博客园
相关推荐

2009-12-30 09:38:37

Silverlight

2016-10-24 23:18:55

数据分析漏斗留存率

2009-12-23 10:46:38

WPF实现用户界面

2010-04-23 13:23:42

Silverlight

2009-12-30 13:51:43

Silverlight

2010-01-28 10:00:54

linux用户注销logout

2012-05-04 09:28:49

Linux

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代码安卓

2016-05-17 10:03:39

用户体验运维可度量

2018-05-30 10:22:47

电商平台

2019-08-22 15:42:03

2009-12-30 10:15:57

Silverlight

2010-08-05 15:06:19

Flex数据绑定

2009-12-30 16:19:49

Silverlight

2023-08-02 09:17:04

2022-01-06 20:31:22

架构MVCTep

2021-09-17 09:00:00

安全身份认证OAuth 2.0
点赞
收藏

51CTO技术栈公众号