自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Windows Phone開發(fā)(14):數(shù)據(jù)模板

移動(dòng)開發(fā)
哪些控件最有可能用到數(shù)據(jù)模板?其實(shí)很多,只要是ContentControl的子類基本上都可以,如 Button等,當(dāng)然,這些控件一般沒(méi)那必要,按鈕多數(shù)情況下顯示一些文本提示用戶用來(lái)干什么的就可以了,頂多你放個(gè)圖標(biāo)在按鈕上,估計(jì)也很少人把一段視頻放在按鈕上吧,呵呵,其實(shí),在WP里面,這是可以的,但沒(méi)有必要。對(duì)的,一般列表形式的控件就最有可能使用到數(shù)據(jù)模板了,比如ListBox控件,如果你的列表控件只是讓用戶看信息的,而不需要額外操作,你完全可以考慮使用ListBox的“老爸”——ItemsControl。

數(shù)據(jù)模板,如果你僅僅聽(tīng)到這個(gè)名詞,你一定很迷惑,什么來(lái)的?用來(lái)干什么的?不急,親,今天,我們一起來(lái)探索一下吧。
用白話文說(shuō),數(shù)據(jù)模板就是用來(lái)規(guī)范數(shù)據(jù)的顯示方式的,關(guān)于模板,估計(jì)各位不陌生的,大家應(yīng)該玩過(guò)PPT吧,都做過(guò)演示文稿吧,對(duì)啊,PPT里面有很多模板 的,明白了吧?不明白?那你一定填過(guò)表吧,如果報(bào)考什么考試的,你肯定會(huì)被要求填一些什么報(bào)名表之類的,或者說(shuō),找過(guò)工用嗎?是啊,做簡(jiǎn)歷也有簡(jiǎn)歷模板。 模板的用法就像做填空題,有了部分規(guī)范的內(nèi)容,然后你按照這個(gè)規(guī)范,在特定的位置填上恰當(dāng)?shù)膬?nèi)容,你總不能說(shuō)把你的姓名填到“性別”那里去吧,這就不符合 規(guī)范了。
好了,廢話講了不少,下面進(jìn)入正題,你想想,哪些控件最有可能用到數(shù)據(jù)模板?哈,其實(shí)很多,只要是ContentControl的子類基本上都可以,如 Button等,當(dāng)然,這些控件一般沒(méi)那必要,按鈕嘛,多數(shù)情況下顯示一些文本提示用戶用來(lái)干什么的就可以了,頂多你放個(gè)圖標(biāo)在按鈕上,估計(jì)也很少人把一 段視頻放在按鈕上吧,呵呵,其實(shí),在WP里面,這是可以的,但沒(méi)有必要。
對(duì)的,一般列表形式的控件就最有可能使用到數(shù)據(jù)模板了,比如ListBox控件,如果你的列表控件只是讓用戶看信息的,而不需要額外操作,你完全可以考慮使用ListBox的“老爸”——ItemsControl。

好,下面我們用一個(gè)例子看看在不自定義數(shù)據(jù)模板的情況下,ItemsControl的列表項(xiàng)是如何顯示的。
首先,當(dāng)然是新建一個(gè)項(xiàng)目了,不用我介紹,相信各位都會(huì)。

  1. <phone:PhoneApplicationPage    
  2.     x:Class="DataTemplateSample.pageA"   
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"   
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"   
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  9.     FontFamily="{StaticResource PhoneFontFamilyNormal}"   
  10.     .......   
  11.     >   
  12.    
  13.     <ItemsControl x:Name="myItemControl"/>   
  14.    
  15. </phone:PhoneApplicationPage>

然后,切換到代碼頁(yè),把myItemControl的數(shù)據(jù)源設(shè)置為一個(gè)字符串?dāng)?shù)組。

  1. public pageA()   
  2. {   
  3.     InitializeComponent();   
  4.    
  5.     this.myItemControl.ItemsSource = new string[] {   
  6.                 "玉米炒蛋",   
  7.                 "燒鴨飯",   
  8.                 "青瓜炒肉",   
  9.                 "水煮豆腐",   
  10.                 "糯米雞"   
  11.     };   
  12. }   

好的,不要流口水啊,現(xiàn)在,你可以運(yùn)行你的超級(jí)項(xiàng)目了。
你應(yīng)該發(fā)現(xiàn)了,列表的每一項(xiàng)都是以文本的方式顯示,其實(shí),它內(nèi)部默認(rèn)就是一個(gè)TextBlock,就是用來(lái)顯示文本的。
那么,如果我設(shè)置的數(shù)據(jù)源不是字符會(huì)怎么樣呢?
好現(xiàn)在看第二個(gè)例子。
先做好布局,和剛才的例子一樣。

  1. <phone:PhoneApplicationPage    
  2.     x:Class="DataTemplateSample.pageB"   
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"   
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"   
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  9.      .......   
  10.     >   
  11.     <ItemsControl Name="myItemsControl" FontSize="52"/>   
  12. </phone:PhoneApplicationPage>  

接著我們定義一個(gè)商品類,包含三個(gè)屬性:商品名稱,單價(jià),條碼。
并把ItemsControl的數(shù)據(jù)源設(shè)置為商品類的集合。

  1. public partial class pageB : PhoneApplicationPage   
  2. {   
  3.     public pageB()   
  4.     {   
  5.         InitializeComponent();   
  6. em.Collections.ObjectModel.ObservableCollection<Goods> goodsList = new System.Collections.ObjectModel.ObservableCollection<Goods>   
  7.         {   
  8.             new Goods{GoodsName="紙飛機(jī)",Price=0.02f,BarCode ="21001475"},   
  9.             new Goods{GoodsName="雞蛋",Price=0.6f,BarCode="21002345"},   
  10.             new Goods{GoodsName="干面包",Price=2.5f,BarCode="21003087"},   
  11.             new Goods{GoodsName="地溝油",Price=33.4f,BarCode="21002020"},   
  12.             new Goods{GoodsName="茅臺(tái)啤酒",Price=108f,BarCode="21009331"}   
  13.         };   
  14.         this.myItemsControl.ItemsSource = goodsList;   
  15.     }   
  16. }   
  17. public class Goods   
  18. {   
  19.     /// <summary>   
  20.     /// 商品價(jià)格   
  21.     /// </summary>   
  22.     public string GoodsName { getset; }   
  23.     /// <summary>   
  24.     /// 商品單價(jià)   
  25.     /// </summary>   
  26.     public float Price { getset; }   
  27.     /// <summary>   
  28.     /// 商品條形碼   
  29.     /// </summary>   
  30.     public string BarCode { getset; }   
  31. }   

運(yùn)行一下,啊,你會(huì)大吃一驚,怎么顯示這內(nèi)容?
(圖1)

http://s3.51cto.com/wyfs01/M01/07/10/wKioOVFuNcWx88u6AACMJKFT_tM005.jpg

 

前文說(shuō)了,數(shù)據(jù)模板默認(rèn)是TextBlock控件,只能顯示文本,那么,當(dāng)它遇到非文本數(shù)據(jù)時(shí),就會(huì)嘗試調(diào)用數(shù)據(jù)源中類型的ToString方法,所以剛 才的示例才會(huì)顯示出類名,這是從Object類繼承過(guò)來(lái)的ToString方法,現(xiàn)在我們把Goods類改一下,重寫它的ToString方法,看看結(jié)果是什么。

  1. public override string ToString()   
  2. {   
  3.     return this.GoodsName;   
  4. }   

這時(shí)候你再運(yùn)行一下,看到商品名稱了吧?

然而,你會(huì)發(fā)現(xiàn),好像還沒(méi)有滿足我們的需求,我們希望每一項(xiàng)中同時(shí)顯示商品名,單價(jià),條碼值,那怎么辦呢?是的,這時(shí)候,就真的要自定義數(shù)據(jù)模板了。

把上面的XAML改一下。

  1. <ItemsControl Name="myItemsControl" FontSize="52">   
  2.     <ItemsControl.ItemTemplate>   
  3.         <DataTemplate>   
  4.             <Grid Margin="0,0,0,27">   
  5.                 <Grid.ColumnDefinitions>   
  6.                     <ColumnDefinition Width="auto"/>   
  7.                     <ColumnDefinition Width="*"/>   
  8.                 </Grid.ColumnDefinitions>   
  9.                 <Grid.RowDefinitions>   
  10.                     <RowDefinition Height="auto"/>   
  11.                     <RowDefinition Height="auto"/>   
  12.                     <RowDefinition Height="auto"/>   
  13.                 </Grid.RowDefinitions>   
  14.                 <TextBlock Grid.Column="0" Grid.Row="0" Text="商品:"/>   
  15.                 <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding GoodsName}"/>   
  16.                 <TextBlock Grid.Column="0" Grid.Row="1" Text="單價(jià):"/>   
  17.                 <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Price}"/>   
  18.                 <TextBlock Grid.Column="0" Grid.Row="2" Text="條碼:"/>   
  19.                 <TextBlock Grid.Column="1" Grid.Row="2" Text="{Binding BarCode}"/>   
  20.             </Grid>   
  21.         </DataTemplate>   
  22.     </ItemsControl.ItemTemplate>   
  23. </ItemsControl>   

好了,現(xiàn)在就基本達(dá)到我們的要求了。
(圖2)

責(zé)任編輯:閆佳明 來(lái)源: oschina
相關(guān)推薦

2013-04-17 14:19:51

Windows PhoWindows Pho

2012-06-06 13:48:34

Windows Pho

2010-04-21 17:07:54

Windows Pho

2013-07-30 12:37:56

Windows PhoWindows Pho

2013-04-16 17:02:50

Windows Pho概論

2013-04-19 16:34:56

Windows PhoWindows Pho

2013-07-30 11:18:37

Windows PhoWindows Pho

2011-06-07 12:42:15

Windows Pho

2013-04-17 14:00:06

Windows PhoWindows Pho

2010-04-08 17:40:23

Windows Pho

2010-08-02 14:47:51

Windows PhoWindows PhoWindows Pho

2010-12-14 18:48:49

微軟

2013-07-31 13:13:50

Windows PhoMVVM模式

2012-06-04 14:47:58

Windows Pho

2013-04-19 15:35:54

Windows Pho隔離存儲(chǔ)

2013-04-19 16:52:24

Windows PhoWindows Pho

2013-07-31 12:50:39

搭建Windows PWindows Pho

2011-06-07 11:35:38

Windows Pho

2012-08-16 10:35:50

Windows Pho

2013-04-17 14:47:19

Windows PhoWindows Pho
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)