wpMVVM模式绑定集合的应用
一、新建一个项目,命名为wpMVVMone,添加一个有关食品信息的类Food.CS,代码如下:
public class Food
{
public string Name { get; set; }
public string Description { get; set; }
public string iconUri { get; set; }
public string Type { get; set; }
}
二、添加一个存放图片的文件夹images,然后往里添加若干张图片。
三、新建一个类:FoodViewModel,并加入命名空间
using System.Collections.ObjectModel;
using System.ComponentModel;
完整代码如下:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel; namespace wpMVVMone
{
public class FoodViewModel:INotifyPropertyChanged
{
private ObservableCollection<Food> _allfood;
public ObservableCollection<Food> Allfood
{
get
{
if (_allfood == null)
_allfood = new ObservableCollection<Food>();
return _allfood;
}
set
{
if (_allfood != value)
{
_allfood = value;
NotifyChanged("Allfood");
}
}
}
public FoodViewModel()
{
try
{
Food item0 = new Food()
{
Name = "西红柿",
iconUri = "images/f01.jpg",
Type = "Healthy",
Description = "西红丝的味道很不错"
};
Food item1 = new Food()
{
Name = "黄瓜",
iconUri = "images/f02.jpg",
Type = "Healthy",
Description = "黄瓜的味道很不错"
};
Food item2 = new Food()
{
Name = "西柿",
iconUri = "images/f03.jpg",
Type = "Healthy",
Description = "西丝的味道很不错"
};
Food item3 = new Food()
{
Name = "西红柿1",
iconUri = "images/f04.jpg",
Type = "Healthy",
Description = "西红丝1的味道很不错"
};
Food item4 = new Food()
{
Name = "西红柿2",
iconUri = "images/f05.jpg",
Type = "Healthy",
Description = "西红丝2的味道很不错"
};
Food item5 = new Food()
{
Name = "西红柿3",
iconUri = "images/f06.jpg",
Type = "Healthy",
Description = "西红丝3的味道很不错"
};
Food item6 = new Food()
{
Name = "西红柿4",
iconUri = "images/f07.jpg",
Type = "Healthy",
Description = "西红丝4的味道很不错"
};
Allfood.Add(item0);
Allfood.Add(item1);
Allfood.Add(item2);
Allfood.Add(item3);
Allfood.Add(item4);
Allfood.Add(item5);
Allfood.Add(item6);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyChanged(string propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
}
}
四、切换到MainPage页面的XAML代码界面,添加引用:xmlns:my="clr-namespace:wpMVVMone"
在外层Grid上边添加
<phone:PhoneApplicationPage.Resources>
<my:FoodViewModel x:Key="food"/>
</phone:PhoneApplicationPage.Resources>
放置内容的Grid中的XAML代码为
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" DataContext="{StaticResource food}">
<ListBox x:Name="listbox1" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Allfood}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="Gray" Width="450" Margin="10">
<Image Source="{Binding iconUri}" Stretch="None"/>
<TextBlock Text="{Binding Name}" FontSize="40" Width="150"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
运行结果如图
wpMVVM模式绑定集合的应用的更多相关文章
- 为 ItemsControl 类型的控件提供行号,mvvm模式 绑定集合
从网络上看到的两种方式,一种是,在 codebehind 里为 控件写事件,下面是将集合绑定到 DataGrid 控件: private void DataGridSoftware_LoadingRo ...
- 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合
[源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...
- 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合
背水一战 Windows 10 之 绑定 通过 Binding 绑定对象 通过 x:Bind 绑定对象 通过 Binding 绑定集合 通过 x:Bind 绑定集合 示例1.演示如何通过 Bindin ...
- WPF 绑定集合 根据集合个数改变样式 INotifyCollectionChanged
问题:当前ListBox Items 绑定 集合数据源ListA时候:ListA集合数据源中存在另外一个集合ListB,当更改或往ListB集合中添加数据的时候,通知改变? 实体类继承 INotify ...
- springmvc:请求参数绑定集合类型
一.请求参数绑定实体类 domain: private String username; private String password; private Double money; private ...
- 在ASP.NET MVC中使用Knockout实践08,使用foreach绑定集合
本篇体验使用 foreach 绑定一个Product集合. 首先使用构造创建一个View Model. var Product = function(data) { this.name = ko.ob ...
- Linux的bond模式绑定及模式区别
[Linux的bond模式配置] 原理: 多块网卡虚拟成一张,实现冗余:多张网卡对外显示一张,具有同一个IP: 工作在网卡是混杂模式的情况下: 对于多物理网卡的 Bond 网卡而言,其中一块物理网卡会 ...
- javascript设计模式(张容铭)学习笔记 - 外观模式绑定事件
有一个需求要为document对象绑定click事件来是想隐藏提示框的交互功能,于是小白写了如下代码: document.onclick = function(e) { e.preventDefaul ...
- WPF—TreeView无限极绑定集合形成树结构
1.如图所示:绑定树效果图 2.前台Xaml代码: <Window x:Class="WpfTest.MainWindow" xmlns="http://schem ...
随机推荐
- C++学习16 继承时的名字遮蔽
如果派生类中的成员变量和基类中的成员变量重名,那么就会遮蔽从基类继承过来的成员变量.所谓遮蔽,就是使用新增的成员变量,而不使用继承来的. 成员函数也一样,如果函数名和参数签名都相同,就会造成遮蔽.如果 ...
- spring注解注入
@Autowired public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } @Test public void tes ...
- [Flex] IFrame系列 —— 嵌入本地页面两种方式source和content(html页面和html代码)
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- [Java] 字符流Reader,读取字符数据
package test.stream; import java.io.BufferedReader; import java.io.FileNotFoundException; import jav ...
- CRM IFRAME 显示地图
作者:卞功鑫 ,转载请保留.http://www.cnblogs.com/BinBinGo/p/5274409.html 需要背景: 现在已经有经纬度,需要在地图上显示出来. 环境: CRM 4.0 ...
- 最最基层的ajax交互代码jquery+java之间的json跨域传递以及java的json代码返回
首先导入jar包 上面的jar包主要是用来将map或list数据转换成json字符串,传递到前台使用. 静态页面的代码:2.html <!DOCTYPE html> <html> ...
- fw:sed的高级用法
转的,找不到原创了.... sed高级用法 <收藏> 首先,应该明白模式空间的定义.模式空间就是读入行所在的缓存,sed对文本行进行的处理都是在这个缓存中进行的.这对接下来 的学习是有帮助 ...
- python 简单实现文件拷贝
1.背景 一日加班需要写一个文件拷贝的函数. 写了几版拷贝函数,有需要的直接粘贴过去 def CopyLocaleFile1(sorfile,desfile): #第一版 sorfp=open(sor ...
- 业务gis 搭建一个skyline 的js模板 (一)
刚刚我们说的是二维的系统,如果要展示三维,我们是不是也需要这样,答案是必须的,是一定要,如果你是基于skyline做三维开发,业务开发人员要去搞那套api估计要吐血,所以我们必须得封装起来,这里不介绍 ...
- JS判断手机浏览器
<script type="text/javascript"> /* * 智能机浏览器版本信息: * */ varbrowser={ versions:function ...