Silverlight2 边学边练 之八 数据绑定

Silverlight2 边学边练 之八 数据绑定,第1张

概述本篇介绍SL2的数据绑定功能,在Silverlight2中数据绑定有3中模式:    * 单向模式(OneWay):源数据更新时目标数据也随之更新。    * 双向模式(TwoWay):源数据或目标数据更新时,彼此相互更新。    * 一次模式(OneTime):只将源数据显示到目标,不用于更新。 单向模式为SL2默认的绑定模式,首先演示一个简单的OneTime模式: XAML Code: < U

本篇介绍SL2的数据绑定功能,在Silverlight2中数据绑定有3中模式: 
  * 单向模式(OneWay):源数据更新时目标数据也随之更新。 
  * 双向模式(TwoWay):源数据或目标数据更新时,彼此相互更新。 
  * 一次模式(OneTime):只将源数据显示到目标,不用于更新。
单向模式为SL2默认的绑定模式,首先演示一个简单的OneTime模式:

XAML Code:

< UserControl  x:Class ="DataBinding.Page"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    WIDth
="300"  Height ="150"  Loaded ="Page_Loaded" >
    
< GrID  x:name ="grIDUser"  Background ="White" >
        
< GrID.RowDeFinitions >
            
< RowDeFinition  Height ="40" ></ RowDeFinition >
            
< RowDeFinition  Height ="40" ></ RowDeFinition >
            
< RowDeFinition  Height ="40" ></ RowDeFinition >
        
</ GrID.RowDeFinitions >
        
< GrID.ColumnDeFinitions >
            
< ColumnDeFinition  WIDth ="80" ></ ColumnDeFinition >
            
< ColumnDeFinition  WIDth ="*" ></ ColumnDeFinition >
        
</ GrID.ColumnDeFinitions >
        
< TextBlock  GrID.Row ="0"  GrID.Column ="0"  Text ="name"  
                   FontSize
="20"  margin ="5,5,5" ></ TextBlock >
        
<!-- 绑定姓名数据 -->
        
< TextBox  margin ="5,5"  borderBrush ="Orange"
                 GrID.Row
="0"  GrID.Column ="1"  FontSize ="20"
                 Text
=" {Binding name} " ></ TextBox >

        
< TextBlock  GrID.Row ="1"  GrID.Column ="0"  Text ="Age"  
                   FontSize
="20"  margin ="5,5" ></ TextBlock >
        
<!-- 绑定年龄数据 -->
        
< TextBox  margin ="5,5"  borderBrush ="Orange"
                 GrID.Row
="1"  GrID.Column ="1"  FontSize ="20"
                 Text
=" {Binding Age} " ></ TextBox >
        
</ GrID >
</ UserControl >


创建User Class:

using  System;
using  System.windows;
using  System.windows.Controls;
using  System.ComponentModel;

namespace  DataBinding
{
    
public   class  User
    {
        
private   string  name;
        
public   string  name
        {
            
get  {  return  name; }
            
set  { name  =  value; }
        }

        
private   string  age;
        
public   string  Age
        {
            
get  {  return  age; }
            
set  { age  =  value; }
        }
    }
}

主程序: using  System;
using  System.Collections.Generic;
using  System.windows;
using  System.windows.Controls;
using  System.windows.documents;

namespace  DataBinding

    
public   partial   class  Page : UserControl
    {
        
public  Page()
        {
            InitializeComponent();
        }

        
private   voID  Page_Loaded( object  sender, RoutedEventArgs e)
        {
            User user 
=   new  User();
            user.name 
=   " Petter " ;
            user.Age 
=   " 20 " ;

            grIDUser.DataContext 
=  user;
        }
    }
}


运行效果:

下面上一个OneWay模式(也适用于TwoWay),XAML Code:

< UserControl  x:Class ="DataBinding.Page"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    WIDth
="400"  Height ="300"  Loaded ="Page_Loaded" >
    
< GrID  x:name ="grIDUser"  Background ="White" >
        
< GrID.RowDeFinitions >
            
< RowDeFinition  Height ="40" ></ RowDeFinition >
            
< RowDeFinition  Height ="40" ></ RowDeFinition >
            
< RowDeFinition  Height ="40" ></ RowDeFinition >
        
</ GrID.RowDeFinitions >
        
< GrID.ColumnDeFinitions >
            
< ColumnDeFinition  WIDth ="80" ></ ColumnDeFinition >
            
< ColumnDeFinition  WIDth ="*" ></ ColumnDeFinition >
        
</ GrID.ColumnDeFinitions >
        
< TextBlock  GrID.Row ="0"  GrID.Column ="0"  Text ="name"  
                   FontSize
="20"  margin ="5,5" ></ TextBlock >
        
<!-- 绑定姓名数据 -->
        
< TextBox  margin ="5,5"  borderBrush ="Orange"
                 GrID.Row
="0"  GrID.Column ="1"  FontSize ="20"
                 Text
=" {Binding name, Mode=OneWay} " ></ TextBox >
        
        
< TextBlock  GrID.Row ="1"  GrID.Column ="0"  Text ="Age"  
                   FontSize
="20"  margin ="5,5" ></ TextBlock >
        
<!-- 绑定年龄数据 -->
        
< TextBox  margin ="5,5"  borderBrush ="Orange"
                 GrID.Row
="1"  GrID.Column ="1"  FontSize ="20"
                 Text
=" {Binding Age, Mode=OneWay} " ></ TextBox >
        
<!-- 点击更新数据 -->
        
< button  GrID.Row ="2"  GrID.Column ="1"  WIDth ="100"  Height ="30"
                Content
="Update Data"  Click ="button_Click" ></ button >
    
</ GrID >
</ UserControl >


User Class 也需要更新,这里要用到接口:INotifyPropertyChanged

using  System;
using  System.windows;
using  System.windows.Controls;
using  System.ComponentModel;

namespace  DataBinding
{
    
public   class  User : INotifyPropertyChanged
    {
        
public   event  PropertyChangedEventHandler PropertyChanged;
        
public   voID  OnPropertyChanged(PropertyChangedEventArgs e)
        {
            
if  (PropertyChanged  !=   null )
                PropertyChanged(
this , e);
        }

        
private   string  name;
        
public   string  name
        {
            
get  {  return  name; }
            
set
            {
                name 
=  value;
                OnPropertyChanged(
new  PropertyChangedEventArgs( " name " ));
            }
        }

        
private   string  age;
        
public   string  Age
        {
            
get  {  return  age; }
            
set
            {
                age 
=  value;
                OnPropertyChanged(
new  PropertyChangedEventArgs( " Age " ));
            }
        }
    }
}


主程序:

using  System;
using  System.Collections.Generic;
using  System.windows;
using  System.windows.Controls;
using  System.windows.documents;

namespace  DataBinding

    
public   partial   class  Page : UserControl
    {
        
public  Page()
        {
            InitializeComponent();
        }

        
private   voID  Page_Loaded( object  sender, RoutedEventArgs e)
        {
            User user 
=   new  User();
            user.name 
=   " Petter " ;
            user.Age 
=   " 20 " ;

            grIDUser.DataContext 
=  user;
        }

        
private   voID  button_Click( object  sender, RoutedEventArgs e)
        {
            User user 
=  (User)grIDUser.DataContext;
            user.name 
=   " Jone " ;
            user.Age 
=   " 28 " ;
        }
    }
}


Demo展示:

本例参考自《Pro Silverlight 2 in C# 2008》CHAPTER 14  DATA BINDING

::源代码下载::

总结

以上是内存溢出为你收集整理的Silverlight2 边学边练 之八 数据绑定全部内容,希望文章能够帮你解决Silverlight2 边学边练 之八 数据绑定所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1047726.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存