MVVM 代码记录
一.XML
<Page
x:Class="MVVM.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MVVM"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" > <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="120*"></RowDefinition>
<RowDefinition Height="500*"></RowDefinition> </Grid.RowDefinitions>
<!--<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>-->
<StackPanel Grid.Row="0">
//绑定数据源 绑定的事viewModel的公有字段
<TextBlock Text="{Binding Path=Title1}" FontSize="30"></TextBlock>
<TextBlock Text="{Binding Path=Title2}" FontSize="60"></TextBlock> </StackPanel>
<Grid x:Name="contentRoot"/>
<ListView Grid.Row="1" ItemsSource="{Binding Collection}" Margin="10,10,-10,10">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontSize="40" Margin="0 ,0,20,0" />
<TextBlock Text="{Binding Gender}" FontSize="40" Margin="0 ,0,20,0" />
<TextBlock Text="{Binding Age}" FontSize="40" Margin="0 ,0,20,0" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page> 二、Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MVVM
{
public class PersonModel
{
public string Name { get; set; }
public char Gender { get; set; }
public int Age { get; set; }
}
}
三、ViewModel
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MVVM
{
//要实现通知 ——>就要实现INotifyPropertyChanged
public class PersonViewModel:INotifyPropertyChanged
{
private string title1;
private string title2;
public PersonViewModel(){}
public PersonViewModel(string Title1, string Title2, IEnumerable<PersonModel> collection)
{
this.title1 = Title1;
this.title2 = Title2;
Collection = new ObservableCollection<PersonModel>();
foreach (var item in collection)
{
Collection.Add(item); } }
//实现接口 + 处理方法
public event PropertyChangedEventHandler PropertyChanged;
private void EventHendle(string propertyNanme)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs (propertyNanme));
}
} public string Title1 { get { return title1; }set { Title1 = value;EventHendle(Title1);}}
public string Title2 { get{return title2;} set{Title2=title2;EventHendle(Title2);}} public ObservableCollection<PersonModel> Collection { get; set; } }
}
结果如下:

MVVM代码记录,来自传智播客公开课
MVVM 代码记录的更多相关文章
- Echarts基于动态数据初步使用 及问题 代码记录.
ECHARTS 插件 基本的动态数据展示(横向图) 下载 echarts.commn.min.js文件 在页面中进行引用, 并为Echarts图形准备一个div盒子 <!-- 引入插件 --&g ...
- OpenCV学习代码记录——人脸检测
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...
- OpenCV学习代码记录—— Snake轮廓
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...
- OpenCV学习代码记录——Hough线段检测
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...
- OpenCV学习代码记录——轮廓(contour)检测
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...
- OpenCV学习代码记录——canny边缘检测
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...
- JSP标签使用的代码记录——《%= %》(神奇的CSDN为啥标题不让打英文的尖括号)
关于JSP的一些标签,在用到的时候有些生疏,就去找了找资源重新温习了一下. 附上两个JSP<%= %>标签的博客,同时也记录当前项目里用到的方法. jsp页面中<%@ %>.& ...
- Shiro权限验证代码记录,正确找到shiro框架在什么地方做了权限识别
权限验证方式的验证代码: org.apache.shiro.web.servlet.AdviceFilter这个类是所有shiro框架提供的默认权限验证实例类的父类 验证代码: public void ...
- CSS代码记录
1. 内容横向滚动的代码 .ul { display: box; display: -webkit-box; width: 250px; background: yellow; overflow-y: ...
随机推荐
- Optimize date2str function
/*************************************************** Created Date: 19 Jul 2013 Created By: Jimmy Xie ...
- 获取枚举Description的Name
/// <summary> /// 获取枚举Description的Name /// </summary> /// <param name="enumName& ...
- 11g RAC r2 的启停命令概述1
目标: 熟悉主要进程的启停顺序 了解独占模式 -excl crsctl start crs与crsctl start cluster 区别 1.熟悉主要进程的启停顺序 1.1 启动节点rac1: [r ...
- [转] 浅谈Microsoft MVP
微软MVP,这个自1993 年开始在社群上出现的计划(MVP Award Program),目前在全球已经累积超过5,000 人,其中在台湾已经有一百多人了,包括我在内,这个计画现在已经成为以微软技术 ...
- linux 目录
创建和删除 int mkdir(const char *pathname, mode_t mode); int rmdir(const char *pathname); 另外remove也可以删除文件 ...
- ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
OS: centos 6.3DB: 5.5.14 测试创建yoon测试表,没有主键,没有索引,基础数据内容如下: mysql> select * from yoon;+----+-------- ...
- Unity Camera属性
Camera属性 1.Clear Flags 清除标记:决定屏幕的那部分将被清除.当使用多个相机来描绘不同的游戏景象时,利用它是非常方便的. 2.Background 背景:在镜头中的所有元素描绘完成 ...
- How to: Add SharePoint 2010 Search Web Parts to Web Part Gallery for Upgraded Site Collections
When you upgrade to Microsoft SharePoint Server 2010, some of the new SharePoint Enterprise Search W ...
- Opencv图像与矩阵的操作
#include "stdafx.h" #include <cv.h> #include <cxcore.h> #include <highgui.h ...
- Xcode Provisioning 路径
~/Library/MobileDevice/Provisioning Profiles