WP7>界面>ListBox数据绑定、图文混编、动态添加行

摘要:
问题:如何使用ListBox进行图文混编我的理解:实际就是对ListBox进行数据绑定(添加数据模板),然后动态添加行(ListItem)示例:前置条件:1)  XAML基础2)  C#基础3)  WP7基础  -˃listbox使用基本知识  -˃工程创建实现:目录:1)  创建一个基本应用2)  插入button及listbox控件3)  给listbox添加Item模板4)  创建Conte

问题:如何使用ListBox进行图文混编

我的理解:实际就是对ListBox进行数据绑定(添加数据模板),然后动态添加行(ListItem)

示例:

WP7>界面>ListBox数据绑定、图文混编、动态添加行第1张

前置条件:

1)  XAML基础

2)  C#基础

3)  WP7基础  -> listbox使用基本知识  ->工程创建

实现:

目录:

1)  创建一个基本应用

2)  插入button及listbox控件

3)  给listbox添加Item模板

4)  创建Content及ContentCollection类

5)  写入事件代码

6)  测试运行

1 创建一个基本应用

1)  创建一个基本的Windows Phone应用程序,OS版本7.0

2)  清除多余的界面元素(留下一个基本的Grid控件)

1 <Grid x:Name="LayoutRoot"Background="Transparent">
2 
3 </Grid>

2 插入button及listbox控件

1     <Grid x:Name="LayoutRoot"Background="Transparent">
2         <Grid.RowDefinitions>
3             <RowDefinition Height="72*" />
4             <RowDefinition Height="696*" />
5         </Grid.RowDefinitions>
6         <Button Content="Add"Height="72"HorizontalAlignment="Left"Margin="12,0,0,0"Name="button1"VerticalAlignment="Top"Width="160"/>
7         <ListBox Grid.Row="1"Name="listBox1">
8 
9         </ListBox>
10     </Grid>

3 给listbox添加Item模板

1)  添加一个Itme模板属性

1   <Grid x:Name="LayoutRoot"Background="Transparent">
2         <Grid.RowDefinitions>
3             <RowDefinition Height="72*" />
4             <RowDefinition Height="696*" />
5         </Grid.RowDefinitions>
6         <Button Content="Add"Height="72"HorizontalAlignment="Left"Margin="12,0,0,0"Name="button1"VerticalAlignment="Top"Width="160" />
7         <ListBox Grid.Row="1"Name="listBox1">
8             <ListBox.ItemTemplate>
9                 <DataTemplate>
10  
11                 </DataTemplate>
12             </ListBox.ItemTemplate>
13         </ListBox>
14     </Grid>

2)  给Itme模板添加样式

(注:Item模板的样式是看不到的,但是你可以将模板样式的代码放到新建页面中去看实际效果)

样式代码:

1                     <Grid Width="455"Height="120">
2                         <Grid.ColumnDefinitions>
3                             <ColumnDefinition Width="Auto" />
4                             <ColumnDefinition Width="Auto" />
5                         </Grid.ColumnDefinitions>
6                         <Image Grid.Column="0"Width="120"Height="120"Stretch="Fill"HorizontalAlignment="Left"VerticalAlignment="Top"/>
7                         <Grid Grid.Column="1"Name="grid1" >
8                             <Grid.RowDefinitions>
9                                 <RowDefinition Height="60*" />
10                                 <RowDefinition Height="60*" />
11                             </Grid.RowDefinitions>
12                             <TextBlock Height="30"HorizontalAlignment="Left"Name="textBlock1"VerticalAlignment="Top"Width="332"Text="123" />
13                             <TextBlock Grid.Row="1"Height="30"HorizontalAlignment="Left"Name="textBlock2"Text="234"VerticalAlignment="Top"Width="332" />
14                         </Grid>
15                     </Grid>

新建一个页面,添加该样式的代码,效果如下:

WP7&gt;界面&gt;ListBox数据绑定、图文混编、动态添加行第2张

给模板添加样式后的代码:

1 <Grid x:Name="LayoutRoot"Background="Transparent">
2         <Grid.RowDefinitions>
3             <RowDefinition Height="72*" />
4             <RowDefinition Height="696*" />
5         </Grid.RowDefinitions>
6         <Button Content="Add"Height="72"HorizontalAlignment="Left"Margin="12,0,0,0"Name="button1"VerticalAlignment="Top"Width="160" />
7         <ListBox Grid.Row="1"Name="listBox1">
8             <ListBox.ItemTemplate>
9                 <DataTemplate>
10                     <Grid Width="455"Height="120">
11                         <Grid.ColumnDefinitions>
12                             <ColumnDefinition Width="Auto" />
13                             <ColumnDefinition Width="Auto" />
14                         </Grid.ColumnDefinitions>
15                         <Image Grid.Column="0"Width="120"Height="120"Stretch="Fill"HorizontalAlignment="Left"VerticalAlignment="Top"/>
16                         <Grid Grid.Column="1"Name="grid1" >
17                             <Grid.RowDefinitions>
18                                 <RowDefinition Height="60*" />
19                                 <RowDefinition Height="60*" />
20                             </Grid.RowDefinitions>
21                             <TextBlock Height="30"HorizontalAlignment="Left"Name="textBlock1"VerticalAlignment="Top"Width="332"Text="123" />
22                             <TextBlock Grid.Row="1"Height="30"HorizontalAlignment="Left"Name="textBlock2"Text="234"VerticalAlignment="Top"Width="332" />
23                         </Grid>
24                     </Grid>
25                 </DataTemplate>
26             </ListBox.ItemTemplate>
27         </ListBox>
28     </Grid>

3)  为Image控件及TextBlock控件添加绑定数据源

为Image控件添加一个新属性source="{Binding Img}" 意思是Image的source属性绑定了Img这个量。(注:source可以是一个图片的网络地址,也可以是一个本地图片的地址)

修改TextBlock控件的Text属性分别为Text="{Binding Time}" 和Text="{Binding Count}",意思是这两个控件的text属性分别绑定了Time量和Count量。

最终界面代码如下

1     <Grid x:Name="LayoutRoot"Background="Transparent">
2         <Grid.RowDefinitions>
3             <RowDefinition Height="72*" />
4             <RowDefinition Height="696*" />
5         </Grid.RowDefinitions>
6         <Button Content="Add"Height="72"HorizontalAlignment="Left"Margin="12,0,0,0"Name="button1"VerticalAlignment="Top"Width="160"Click="button1_Click" />
7         <ListBox Grid.Row="1"Name="listBox1">
8             <ListBox.ItemTemplate>
9                 <DataTemplate>
10                     <Grid Width="455"Height="120">
11                         <Grid.ColumnDefinitions>
12                             <ColumnDefinition Width="Auto" />
13                             <ColumnDefinition Width="Auto" />
14                         </Grid.ColumnDefinitions>
15                         <Image Grid.Column="0"Width="120"Height="120"Stretch="Fill"HorizontalAlignment="Left"VerticalAlignment="Top"Source="{Binding Img}"/>
16                         <Grid Grid.Column="1"Name="grid1" >
17                             <Grid.RowDefinitions>
18                                 <RowDefinition Height="60*" />
19                                 <RowDefinition Height="60*" />
20                             </Grid.RowDefinitions>
21                             <TextBlock Height="30"HorizontalAlignment="Left"Name="textBlock1"VerticalAlignment="Top"Width="332"Text="{Binding Time}" />
22                             <TextBlock Grid.Row="1"Height="30"HorizontalAlignment="Left"Name="textBlock2"Text="{Binding Count}"VerticalAlignment="Top"Width="332" />
23                         </Grid>
24                     </Grid>
25                 </DataTemplate>
26             </ListBox.ItemTemplate>
27         </ListBox>
28     </Grid>

4 创建Content及ContentCollection类

1)  在界面中,Item模板绑定了3个量Img、Time、Count,对应的我们新建一个Content类

1 usingSystem;
2 usingSystem.Net;
3 usingSystem.ComponentModel;
4 usingSystem.Runtime.Serialization;
5 usingSystem.Collections.ObjectModel;
6 
7 namespacePhoneApp1
8 {
9     public classContent
10 {
11         private stringtime;
12         private stringcount;
13         private stringimg;
14 
15         public stringTime
16 {
17             get { returntime; }
18             set
19 {
20                 if (time !=value)
21                     time =value;
22                 NotifyPropertyChanged("Time");
23 }
24 }
25 
26         public stringCount
27 {
28             get { returncount; }
29             set
30 {
31                 if (count !=value)
32                     count =value;
33                 NotifyPropertyChanged("Count");
34 }
35 }
36 
37         public stringImg
38 {
39             get { returnimg; }
40             set
41 {
42                 if (img !=value)
43                     img =value;
44                 NotifyPropertyChanged("Img");
45 }
46 }
47 
48         public eventPropertyChangedEventHandler PropertyChanged;
49         private void NotifyPropertyChanged(stringinfo)
50 {
51             if (PropertyChanged != null)
52 {
53                 PropertyChanged(this, newPropertyChangedEventArgs(info));
54 }
55 }
56 }
57 }

注:其中NotifyPropertyChanged("Img")中的“Img”对应的就是在界面中Source="{Binding Img}"中的Img

2)  然后新建一个ContentCollection类

1 usingSystem;
2 usingSystem.Net;
3 usingSystem.ComponentModel;
4 usingSystem.Runtime.Serialization;
5 usingSystem.Collections.ObjectModel;
6 
7 namespacePhoneApp1
8 {
9     public class ContentCollection : ObservableCollection<Content>
10 {
11         publicContentCollection()
12             : base()
13 {
14 }
15 }
16 }

5 写入事件代码

1     public partial classMainPage : PhoneApplicationPage
2 {
3 ContentCollection mySource;
4         int count = 0;
5         //构造函数
6         publicMainPage()
7 {
8 InitializeComponent();
9             mySource = newContentCollection();
10             listBox1.ItemsSource =mySource;
11 }
12 
13         private void button1_Click(objectsender, RoutedEventArgs e)
14 {
15             Content content = newContent();
16             content.Img = "http://pic.cnblogs.com/face/u97911.jpg";
17             content.Time =DateTime.Now.ToString();
18             content.Count =count.ToString();
19             count++;
20 mySource.Add(content);
21 }
22     }

6 测试运行

点击Add按钮若干次

WP7&gt;界面&gt;ListBox数据绑定、图文混编、动态添加行第3张

说明:

listbox图文混编有几个要点:

1)  设置其Item的样式

2)  创建一个对应的实体数据Content

3)  使用mySource使界面更新

WP7&gt;界面&gt;ListBox数据绑定、图文混编、动态添加行第4张

免责声明:文章转载自《WP7&amp;gt;界面&amp;gt;ListBox数据绑定、图文混编、动态添加行》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇django的几种方法进行序列化(视图)Swing菜单与工具栏(三)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

IntelliJ IDEA 版本控制(svn、git) 修改文件后,所属目录的颜色也变化

IntelliJIDEA的版本控制默认文件已被修改,目录的颜色不会改变,这很不方便。例如,修改方法如下:文件---&gt;设置--&gt;版本控制--&gt;选中“显示更改内容的目录”,效果如下:...

Linux查看机器和硬盘的SN

查看硬件RAID中硬盘的SN#sas端口:[root@~]$smartctl-a/dev/sda dmegaraid,n***序列号:6RJ974SR***#sat端口[root@~]$smarttl-a/dev/sda-dsat+megaraid,n***序列号:6BRJ974SR***查看机器SN[root@~~]$dmidcode-t1**序列号...

JAVA 实现CLOB转String

CLOB定义了用于在数据库中保存文件的类型。SQLCLOB是一种内置类型,它将一个大型字符对象作为列值存储在数据库表的一行中。默认情况下,驱动程序使用SQLlocator实现Clob对象,这意味着Clob对象包含指向SQLCLOB数据的逻辑指针,而不是数据本身。Clob对象在其创建的事务期间有效。在一些数据库系统中,文本也用作CLOB的别名。例如,SQL S...

支付宝支付api

使用:alipayDemo来配置支付宝支付接口1拿到商户号,回调地址,支付宝公钥,我的私钥---生成一个对象#给支付宝发请求,信息要用支付宝公钥加密#支付宝给我响应信息,信息会用商户的公钥加密,回来之后再拿用户私钥解密2对象.direct_pay传支付金额,支付商品描述,支付订单号---返回个加密的串3拿到加密的串拼到get请求参数部分pay_url="ht...

allure报告实现保存失败用例截图功能

allure中可以保存日志信息和截图日志allure能够自动识别。截图需要自己在添加allure方法。...

其他查询

如果有其他关联表使用此ID,则无需从数据库中再次查找。1) 查询表是否具有SELECTOBJECT_ID相当于以下语句:SELECTidFROMsysobjectsWHERE Name=N'Students and type=N'U'2)通常用于在创建表和视图时进行决策。...