Silverlight单向绑定相关应用技巧解析
创始人
2024-06-18 17:40:54
0

Silverlight开发工具是微软公司在进军UI领域的一个攻坚利器。它的出现,改变了开发人员传统的变成模式,使开发人员也能补通过美工来实现各种多媒体相关功能需求。在这里我们就先来了解下Silverlight单向绑定的一些相关概念。#t#

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

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

 

  1. public class User : INotify
    PropertyChanged  
  2. {  
  3. public event PropertyChangedEven
    tHandler PropertyChanged;  
  4. private string _name;  
  5. public string Name  
  6. {  
  7. get { return _name; }  
  8. set   
  9. {  
  10. _name = value;  
  11. if(PropertyChanged != null)  
  12. {  
  13. PropertyChanged(this, new Property
    ChangedEventArgs("Name"));  
  14. }  
  15. }  
  16. }  
  17. private string _address;  
  18. public string Address  
  19. {  
  20. get { return _address; }  
  21. set   
  22. {  
  23. _address = value;  
  24. if (PropertyChanged != null)  
  25. {  
  26. PropertyChanged(this, new Property
    ChangedEventArgs("Address"));  
  27. }  
  28. }  
  29. }  

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

  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. < Button x:Name="btnUpdate" 
    Width="100" Height="40" 
  19. Content="Update" Click="btnUpdate_Click"/> 
  20. < TextBlock Foreground="White" 
    FontSize="18" Text="姓名:" 
  21. Grid.Row="1" Grid.Column="0" 
    HorizontalAlignment="Right"/> 
  22. < TextBlock x:Name="lblName" 
    Foreground="White" FontSize="18" 
  23. Grid.Row="1" Grid.Column="1" 
    HorizontalAlignment="Left" 
  24. Text="{Binding Name, Mode=OneWay}"/> 
  25. < TextBlock Foreground="White" 
    FontSize="18" Text="位置:" 
  26. Grid.Row="2" Grid.Column="0" 
    HorizontalAlignment="Right"/> 
  27. < TextBlock x:Name="lblAddress" 
    Foreground="White" FontSize="18" 
  28. Grid.Row="2" Grid.Column="1" 
    HorizontalAlignment="Left" 
  29. Text="{Binding Address, Mode=OneWay}"/> 
  30. < /Grid> 

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

 

  1. public partial class Page : UserControl  
  2. {  
  3. public Page()  
  4. {  
  5. InitializeComponent();  
  6. }  
  7. User user;  
  8. private void UserControl_Loaded(object 
    sender, RoutedEventArgs e)  
  9. {  
  10. user = new User();  
  11. user.Name = "TerryLee";  
  12. user.Address = "中国 天津";  
  13. lblName.DataContext = user;  
  14. lblAddress.DataContext = user;  
  15. }  
  16. private void btnUpdate_Click(object 
    sender, RoutedEventArgs e)  
  17. {  
  18. user.Name = "李会军";  
  19. user.Address = "China Tianjin";  
  20. }  

Silverlight单向绑定的应用方法就为大家介绍这里,希望能帮助大家提高对Silverlight这个工具的了解程度。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...