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: ...
随机推荐
- 浅析php fwrite写入txt文件的时候用 \r\n不能换行的问题
以下是对php中fwrite写入txt文件的时候用 \r\n不能换行的问题进行了介绍,需要的朋友可以过来参考下今天遇到了一个问题就是用fwrite写入txt文件的时候用 rn不能换行试了很久都没找到办 ...
- Struct是干什么的
对于结构(Struct)这一看起来比较特殊的东西(用的比较少,只好用东西来形容了),真心用得少,只有在被问起的时候,才会想起,看看它到底是什么吧. 先给一个链接:http://www.cnblogs. ...
- Docs list
http://www.deansys.com/doc/ldd3/index.html Github中文文档: http://www.worldhello.net/gotgithub/03-projec ...
- Python编码与解码
# -*- coding: utf-8 -*- # 直接保存为Python脚本,对照执行结果会好看点. # 实验的内容都是在Python 2.7.x下进行的. # Python3默认采用unicode ...
- Winform上传下载文件代码
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...
- 绘制dot 图
常用参数 格式:dot -T<type> -o<outfile> <infile.dot> 输入文件是<infile.dot>,生成的格式由<ty ...
- Mac OS X 10.10.2 Yosemite jdk 环境变量配置
我的Mac系统版本是OS X 10.10.2 Yosemite,为了用Eclipse做android开发,安装了jdk 1.7, 但是如果想使用IntelliJ IDE做android开发的话,就需要 ...
- Android 中的WiFi剖析
Android的WiFi 我们通常看到WiFi的守护进程wpa_supplicant在我们的ps的进程列表中,这个就是我们的wifi守护进程.wpa_supplicant在external/wpa_s ...
- floor舍去法取整
$int = 0.99999999999999999; echo floor($int); // returns 1 $int = 0.9999999999999999; echo floor($in ...
- phpstorm8 设置及license key
phpstorm8 license key Learn Programming ===== LICENSE BEGIN ===== 63758-12042010 00000Ryqh0NCC73lpRm ...