一、新建一个项目,命名为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模式绑定集合的应用的更多相关文章

  1. 为 ItemsControl 类型的控件提供行号,mvvm模式 绑定集合

    从网络上看到的两种方式,一种是,在 codebehind 里为 控件写事件,下面是将集合绑定到 DataGrid 控件: private void DataGridSoftware_LoadingRo ...

  2. 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合

    [源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...

  3. 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合

    背水一战 Windows 10 之 绑定 通过 Binding 绑定对象 通过 x:Bind 绑定对象 通过 Binding 绑定集合 通过 x:Bind 绑定集合 示例1.演示如何通过 Bindin ...

  4. WPF 绑定集合 根据集合个数改变样式 INotifyCollectionChanged

    问题:当前ListBox Items 绑定 集合数据源ListA时候:ListA集合数据源中存在另外一个集合ListB,当更改或往ListB集合中添加数据的时候,通知改变? 实体类继承 INotify ...

  5. springmvc:请求参数绑定集合类型

    一.请求参数绑定实体类 domain: private String username; private String password; private Double money; private ...

  6. 在ASP.NET MVC中使用Knockout实践08,使用foreach绑定集合

    本篇体验使用 foreach 绑定一个Product集合. 首先使用构造创建一个View Model. var Product = function(data) { this.name = ko.ob ...

  7. Linux的bond模式绑定及模式区别

    [Linux的bond模式配置] 原理: 多块网卡虚拟成一张,实现冗余:多张网卡对外显示一张,具有同一个IP: 工作在网卡是混杂模式的情况下: 对于多物理网卡的 Bond 网卡而言,其中一块物理网卡会 ...

  8. javascript设计模式(张容铭)学习笔记 - 外观模式绑定事件

    有一个需求要为document对象绑定click事件来是想隐藏提示框的交互功能,于是小白写了如下代码: document.onclick = function(e) { e.preventDefaul ...

  9. WPF—TreeView无限极绑定集合形成树结构

    1.如图所示:绑定树效果图 2.前台Xaml代码: <Window x:Class="WpfTest.MainWindow" xmlns="http://schem ...

随机推荐

  1. 三种主流的WebService实现方案(REST/SOAP/XML-RPC)简述及比较

    目前知道的三种主流的Web服务实现方案为:REST:表象化状态转变 (软件架构风格)SOAP:简单对象访问协议 XML-RPC:远程过程调用协议 简单介绍: REST:表征状态转移(Represent ...

  2. 把Nginx加为系统服务(service nginx start/stop/restart)

    1.编写脚本,名为nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - ...

  3. SQL Server 2005中的分区表(五):添加一个分区(转)

    所谓天下大事,分久必合,合久必分,对于分区表而言也一样.前面我们介绍过如何删除(合并)分区表中的一个分区,下面我们介绍一下如何为分区表添加一个分区. 为分区表添加一个分区,这种情况是时常会 发生的.比 ...

  4. C++学习39 异常处理入门(try和catch)

    编译器能够保证代码的语法是正确的,但是对逻辑错误和运行时错误却无能为力,例如除数为 0.内存分配失败.数组越界等.这些错误如果放任不管,系统就会执行默认的操作,终止程序运行,也就是我们常说的程序崩溃( ...

  5. struts (四) path DMI

    1.path 常使用绝对路径 path = request.getContextPath(); basepath = request.getscheme+"://"+request ...

  6. $.ajax()引发的对Deferred的总结

    传统的ajax写法: $.ajax({ url:"1.json", type:"get", success:function(data){}, error:fu ...

  7. Mac OS X安装Redis

    http://my.oschina.net/jackieyeah/blog/524583

  8. FreeMarker中List排序

    有时候需要在页面上对list排序,虽然也可以在后台代码中完成,但这个可能要看具体情况.排序的样本代码如下: <#list resultMap.topViewList?sort_by(" ...

  9. nyoj 56-阶乘因式分解(一)

    点击打开链接 阶乘因式分解(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 给定两个数m,n,其中m是一个素数. 将n(0<=n<=10000)的阶乘分 ...

  10. Bootstrap栅格系统

    栅格系统分为两种:默认栅格系统 row,流式栅格系统 row-fluid. row 默认栅格系统:即指定了每个栅格的宽度为60px,间距为20px.共有12个栅格.总宽度为940px; 即12个栅格= ...