天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

一步一步學Silverlight :數(shù)據(jù)綁定

概念

Silverlight 2 Beta 1版本發(fā)布了,無論從Runtime還是Tools都給我們帶來了很多的驚喜,如支持框架語言Visual Basic, Visual C#, IronRuby, IronPython,對JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步學Silverlight 2系列》文章帶您快速進入Silverlight 2開發(fā)。

本文為系列文章第十一篇,主要介紹Silverlight 2中的數(shù)據(jù)綁定。

數(shù)據(jù)綁定模式

在Silverlight 2中,支持三種模式的數(shù)據(jù)綁定。

1.ONETime:一次綁定,在綁定創(chuàng)建時使用源數(shù)據(jù)更新目標,適用于只顯示數(shù)據(jù)而不進行數(shù)據(jù)的更新。

2.OneWay:單向綁定,在綁定創(chuàng)建時或者源數(shù)據(jù)發(fā)生變化時更新到目標,適用于顯示變化的數(shù)據(jù)。

3.TwoWay:雙向綁定,在任何時候都可以同時更新源數(shù)據(jù)和目標。

Jesse Liberty舉的例子非常的形象,使用Silverlight開發(fā)一個在線書店,顯示書籍的書名、作者等信息,使用ONETime模式,這些數(shù)據(jù)一般不會發(fā)生變化的;顯示價格信息時使用OneWay模式,因為管理員可能會在一天內調整價格;顯示書籍的剩余數(shù)量時用TwoWay模式,數(shù)量隨著用戶的訂購會隨時發(fā)生變化,即目標和源數(shù)據(jù)都要進行更新。

簡單數(shù)據(jù)綁定

在本示例中我們將做一個簡單的數(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>

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

public class User{    public string Name { get; set; }    public string Address { get; set; }}

使用綁定句法{Binding Property}進行數(shù)據(jù)綁定,注意下面的兩個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)建一個User的實例并賦值后,把user實例綁定到了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;}

運行示例后,可以看到:

TerryLee_Silverlight2_0055

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

 

 

單向綁定示例

如果需要在數(shù)據(jù)源發(fā)生變化時能夠通知UI進行相應的更新,即使用單向綁定OneWay或者雙向綁定TwoWay,則業(yè)務實體需要實現(xiàn)接口INotifyPropertyChanged。在本示例中,我們加上一個更新按鈕,當單擊按鈕時更新user實例的屬性值,會看到界面上的數(shù)據(jù)也會發(fā)生變化。

修改一下User類,使其實現(xiàn)INotifyPropertyChanged接口。

public class User : INotifyPropertyChanged{    public event PropertyChangedEventHandler PropertyChanged;    private string _name;    public string Name    {        get { return _name; }        set         {            _name = value;            if(PropertyChanged != null)            {                PropertyChanged(this, new PropertyChangedEventArgs("Name"));            }        }    }    private string _address;    public string Address    {        get { return _address; }        set         {            _address = value;            if (PropertyChanged != null)            {                PropertyChanged(this, new PropertyChangedEventArgs("Address"));            }        }    }}

修改數(shù)據(jù)綁定模式,使用單向綁定OneWay模式,如{Binding Address, Mode=OneWay}

<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"/>    <Button x:Name="btnUpdate" Width="100" Height="40"            Content="Update" Click="btnUpdate_Click"/>    <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, Mode=OneWay}"/>    <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, Mode=OneWay}"/></Grid>

編寫事件處理程序,為了演示把user聲明為一個全局的,并在按鈕的單擊事件中修改其屬性值:

public partial class Page : UserControl{    public Page()    {        InitializeComponent();    }    User user;    private void UserControl_Loaded(object sender, RoutedEventArgs e)    {        user = new User();        user.Name = "TerryLee";        user.Address = "中國 天津";        lblName.DataContext = user;        lblAddress.DataContext = user;    }    private void btnUpdate_Click(object sender, RoutedEventArgs e)    {        user.Name = "李會軍";        user.Address = "China Tianjin";    }}

運行后如下所示:

TerryLee_Silverlight2_0056

單擊Update按鈕后:

TerryLee_Silverlight2_0057

綁定到列表

下面再看一個綁定到列表的簡單例子,一般都會使用DataGrid或者ListBox來進行列表數(shù)據(jù)的顯示。下面的示例我們顯示一個文章列表:

<Grid Background="#46461F">    <Grid.RowDefinitions>        <RowDefinition Height="40"></RowDefinition>        <RowDefinition Height="*"></RowDefinition>    </Grid.RowDefinitions>    <Grid.ColumnDefinitions>        <ColumnDefinition></ColumnDefinition>    </Grid.ColumnDefinitions>    <Border Grid.Row="0" Grid.Column="0" CornerRadius="15"            Width="240" Height="36" Background="Orange"            Margin="20 0 0 0" HorizontalAlignment="Left">        <TextBlock Text="文章列表" Foreground="White"                   HorizontalAlignment="Left" VerticalAlignment="Center"                   Margin="20 0 0 0"></TextBlock>    </Border>    <ListBox x:Name="PostList" Grid.Column="0" Grid.Row="1"             Margin="40 10 10 10"             HorizontalContentAlignment="Left" VerticalContentAlignment="Bottom"             ItemsSource="{Binding Posts}">    </ListBox></Grid>

編寫一個簡單的業(yè)務類:

public class Blog{    public List<String> Posts { get; set; }}

初始化集合數(shù)據(jù)并進行綁定

private void UserControl_Loaded(object sender, RoutedEventArgs e){    Blog blog = new Blog();    blog.Posts = new List<String>    {        "一步一步學Silverlight 2系列(10):使用用戶控件",        "一步一步學Silverlight 2系列(9):使用控件模板",        "一步一步學Silverlight 2系列(8):使用樣式封裝控件觀感",        "一步一步學Silverlight 2系列(7):全屏模式支持"    };    PostList.DataContext = blog;}

最終運行的結果如下所示:

TerryLee_Silverlight2_0058

當然我們也可以使用ListBox的ItemsSource屬性進行綁定,

結束語

本文簡單介紹了Silverlight 2中的數(shù)據(jù)綁定,你可以從這里下載文章示例代碼。

NET技術一步一步學Silverlight :數(shù)據(jù)綁定,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 极品虎白在线观看 | 日产精品高潮呻吟AV久久 | 青青草A在在观免费线观看 青青草AV国产精品 青青草 久久久 | 99免费精品 | 国产主播福利一区二区 | 2020精品极品国产色在线 | 91夫妻交友论坛 | 人妻中文字幕无码系列 | 久久精品热99看二 | 有人有片的观看免费视频 | 2019久久视频这里有精品15 | 中文字幕亚洲欧美日韩2o19 | 亚洲欧美免费无码专区 | 伊人久久大香线蕉影院95 | 亚洲AV无码影院在线播放 | 亚洲伊人色综合久久天天伊人 | 久久偷拍免费2017 | 受喷汁红肿抽搐磨NP双性 | 国产剧情在线精品视频不卡 | 久久理伦片琪琪电影院 | 老司机亚洲精品影院在线观看 | 兰桂坊人成社区亚洲精品 | 国产午夜精品自在自线之la | 国产精品自在在线午夜精品 | 日本枯瘦娇小 | 在线精品视频免费观看 | 亚洲中文字幕无码一去台湾 | 午夜福利体验试看120秒 | 免费看午夜高清性色生活片 | 国产一区私人高清影院 | 9久久99久久久精品齐齐综合色圆 | 中文字幕久精品视频在线观看 | 久久99亚洲热最新地址获取 | 九九大香尹人视频免费 | 特级毛片全部免费播放免下载 | 老司机深夜福利ae 入口网站 | 成人免费无毒在线观看网站 | 日韩欧美中文字幕在线二视频 | 粗大分开挺进内射 | 色拍拍噜噜噜啦啦新网站 | 99re8热视频这在线视频 |