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++学习37 string字符串的访问和拼接
访问字符串中的字符 string 字符串也可以像字符串数组一样按照下标来访问其中的每一个字符.string 字符串的起始下标仍是从 0 开始.请看下面的代码: #include <iostrea ...
- Ext vtype
//form验证中vtype的默认支持类型1.alpha //只能输入字母,无法输入其他(如数字,特殊符号等)2.alphanum//只能输入字母和数字,无法输入其他3.email//email验证, ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- php文字水印和php图片水印实现代码
本文章向码农们介绍php文字水印和php图片水印实现代码,需要的码农可以参考一下. php 文字水印 文字水印就是在图片上加上文字,主要使用gd库的imagefttext方法,并且需要字体文件. 实现 ...
- Begin using git (Part1) - Git的安装与配置
Git提供了适用于Linux, Windows, OSX的客户端, 本节以Windows为例介绍基本安装与配置. 所需工具:msysgit, kdiff3. Get windows installer ...
- 使用OpenCV/python进行双目测距
在做SLAM时,希望用到深度图来辅助生成场景,所以要构建立体视觉,在这里使用OpenCV的Stereo库和python来进行双目立体视觉的图像处理. 立体标定 应用标定数据 转换成深度图 标定 在开始 ...
- POI实现word文档转html文件
POI word文件转html package com.feiruo.officeConvert; import java.io.BufferedWriter; import java.io.File ...
- Flex4 自定义通用的ImageButton
Flex4与之前版本的一个极大区别就是外观皮肤的分离,虽然进一步解耦,但存在一个不爽的地方就是增加了编码的工作量,你能想象为你的每个自定义组件都写一个对应的皮肤吗?可能仅仅和你之前写过的组件差了那么一 ...
- 【测试】使用hr用户下的employees表写一条SQL语句,执行计划走索引全扫描
SQL> select count(*) from employees; COUNT(*) ---------- Execution Plan ------------------------- ...
- 为知笔记 Markdown 新手指南
为知笔记 Markdown 新手指南 http://www.wiz.cn/feature-markdown.html 时序图,流程图详细流程图语法 http://adrai.github.io/flo ...