<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      一步一步學(xué)Silverlight 2系列(11):數(shù)據(jù)綁定

      概念

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

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

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

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

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

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

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

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

      簡單數(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}進(jìn)行數(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的實(shí)例并賦值后,把user實(shí)例綁定到了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;
      }

      運(yùn)行示例后,可以看到:

      TerryLee_Silverlight2_0055

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

      單向綁定示例

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

      修改一下User類,使其實(shí)現(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";
          }
      }

      運(yùn)行后如下所示:

      TerryLee_Silverlight2_0056

      單擊Update按鈕后:

      TerryLee_Silverlight2_0057

      綁定到列表

      下面再看一個綁定到列表的簡單例子,一般都會使用DataGrid或者ListBox來進(jìn)行列表數(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è)務(wù)類:

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

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

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

      最終運(yùn)行的結(jié)果如下所示:

      TerryLee_Silverlight2_0058

      當(dāng)然我們也可以使用ListBox的ItemsSource屬性進(jìn)行綁定,

      結(jié)束語

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

      下一篇:一步一步學(xué)Silverlight 2系列(12):數(shù)據(jù)與通信之WebClient

      posted @ 2008-03-08 23:48  TerryLee  閱讀(43648)  評論(86)    收藏  舉報
      主站蜘蛛池模板: 国产精品午夜无码AV天美传媒 | 国产内射性高湖| 乱子伦视频在线看| 国产精品男女爽免费视频| 曰韩无码av一区二区免费| 国产特色一区二区三区视频| 日韩人妻无码一区二区三区99| 精品无码国产污污污免费| 成人免费在线播放av| 强开少妇嫩苞又嫩又紧九色| 日本人一区二区在线观看| 国产精品自在拍首页视频8| 亚洲性猛交xxxx| 热久久这里只有精品国产| 国产曰批视频免费观看完| 精品国产迷系列在线观看| 成人午夜污一区二区三区| 亚洲男人AV天堂午夜在| 国产草草影院ccyycom| 国产精品香港三级国产av| 久久精品免费无码区| 国产一区二区三区高清视频| 四虎成人精品永久网站| 忻城县| 久久夜色撩人精品国产av| 亚洲熟妇少妇任你躁在线观看无码| 国产亚洲精品一区二区不卡| 福利一区二区在线视频| 午夜福利片1000无码免费| 亚洲综合无码一区二区三区不卡| 国产精品久久精品| 黑巨人与欧美精品一区| 国模冰莲自慰肥美胞极品人体图| 九九热在线观看视频精品| 99精品热在线在线观看视| 亚洲乱码一卡二卡卡3卡4卡| 日韩有码中文字幕av| 高潮迭起av乳颜射后入| 麻豆蜜桃av蜜臀av色欲av| 欧美性猛交xxxx乱大交极品| 成人午夜在线观看日韩|