wpf中数据绑定和INotifyPeropertyChanged的理解
原创:转载请注明出处。
先说数据绑定:
XAML代码:
<Window x:Class="数据绑定和INotifyPropertyChanged.Window1" Loaded="Window_Loaded"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="数据绑定和INotifyPropertyChanged" Height="" Width="">
<Grid>
<Label Height="" Margin="15,35,0,0" Name="lbAge" VerticalAlignment="Top" HorizontalAlignment="Left" Width="">年龄:</Label>
<Label Height="" HorizontalAlignment="Left" Margin="15,83,0,0" Name="lbName" VerticalAlignment="Top" Width="">姓名:</Label>
<TextBox Height="" Margin="79,37,79,0" Name="TxtAge" VerticalAlignment="Top" Text="{Binding Name}"/>
<TextBox Height="" Margin="79,83,79,0" Name="TxtName" VerticalAlignment="Top" Text="{Binding Age}"/>
</Grid>
</Window>
C#代码:
步骤如下:
先定义一个类
public class Person:INotifyPropertyChanged
{
private string _name; public string Name
{
get { return _name; }
set { _name = value; }
} private int _age; public int Age
{
get { return _age; }
set { _age = value; }
}
18 }
然后在后台写代码:
private Person p1;
public Person P1
{
get { return p1; }
set { p1 = value; }
}
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
p1 = new Person();
p1.Name = "aaa";
p1.Age = ;
TxtAge.DataContext = p1;
TxtName.DataContext = p1;
}
注意:
这里牵扯到一个很重要的东西,“数据上下文”,即DataContext,必须把界面控件的DataContext和类的实例绑定起来,这样界面才会显示类中属性的值。
要想控件获得类中的属性值并显示,控件必须绑定类中的属性。
接下来说一下INotifyPropertyChanged,这个是MVVM的基础,也是数据双向绑定很重要很关键的部分。通过他,类的属性值改变才会改变UI界面显示的值。(有一个很重要的知识点:事件,这个不懂得赶紧去学下再来接着往下看)。
上代码,这个代码是数据绑定部分代码的基础上进一步完善。
XMAL代码:
<Window x:Class="数据绑定和INotifyPropertyChanged.Window1" Loaded="Window_Loaded"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="数据绑定和INotifyPropertyChanged" Height="300" Width="300">
<Grid>
<Label Height="28" Margin="15,35,0,0" Name="lbAge" VerticalAlignment="Top" HorizontalAlignment="Left" Width="42">年龄:</Label>
<Label Height="28" HorizontalAlignment="Left" Margin="15,83,0,0" Name="lbName" VerticalAlignment="Top" Width="42">姓名:</Label>
<TextBox Height="23" Margin="79,37,79,0" Name="TxtAge" VerticalAlignment="Top" Text="{Binding Name}"/>
<TextBox Height="23" Margin="79,83,79,0" Name="TxtName" VerticalAlignment="Top" Text="{Binding Age}"/>
<Button Height="23" HorizontalAlignment="Left" Margin="30,0,0,80" Name="BtnAgePP" VerticalAlignment="Bottom" Width="75" Click="BtnAgePP_Click">Age++</Button>
<Button Height="23" Margin="132,0,71,80" Name="BtnDisplayAge" VerticalAlignment="Bottom" Click="BtnDisplayAge_Click">显示年龄</Button>
</Grid>
</Window>
C#代码:
private Person p1;
public Person P1
{
get { return p1; }
set { p1 = value; }
}
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
p1 = new Person();
p1.Name = "aaa";
p1.Age = 15;
TxtAge.DataContext = p1;
TxtName.DataContext = p1;
}
private void BtnDisplayAge_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(p1.Age.ToString());
MessageBox.Show(p1.Name);
}
private void BtnAgePP_Click(object sender, RoutedEventArgs e)
{
p1.Age++;
p1.Name = "ccc";
}
类:person也要改,
public class Person:INotifyPropertyChanged
{
private string _name; public string Name
{
get { return _name; }
set { _name = value; if (PropertyChanged != null) { PropertyChanged(this,new PropertyChangedEventArgs("Name")); } }
} private int _age; public int Age
{
get { return _age; }
set { _age = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Age")); } }
} #region INotifyPropertyChanged 成员 public event PropertyChangedEventHandler PropertyChanged;//数据绑定会监听这个PropertyChanged事件 #endregion
}
有没有注意到类Person有什么变化,对,Person类实现了INotifyPropertyChanged接口。
解释一下:
1、Person类实现INotifyPropertyChanged接口,这个接口只有一个PropertyChanged事件成员。
看源码:
// 摘要:
// 向客户端发出某一属性值已更改的通知。
public interface INotifyPropertyChanged
{
// 摘要:
// 在更改属性值时发生。
event PropertyChangedEventHandler PropertyChanged;
}
2、数据绑定会检测Person类是否实现了INotifyPropertyChanged接口,
3、如果Person类实现了INotifyPropertyChanged接口,那就监听PropertyChanged事件。
4、如果Property改变了,那么就产生PropertyChanged事件,即PropertyChanged!=null。
5、向前端UI发送属性值改变的额通知,控件绑定了属性,控件收到属性改变的通知,就把自身的值也改变 。
补充:在数据绑定的时候,是一个一个控件绑定的,当控件少的时候这没问题,但是控件多的话,就要重复写很多次的datacontext了。这时候,可以用this.DataContext来统一指定当前窗口的所有控件的数据上下文。
譬如,本文中的例子可以变成this.DataContext = p1。这样的话,DataContext就可以作用到全部的控件,而不需要一个一个去指定了。
当然,也可以在this的DataContext指定后,给某个控件指定DataContext,这样子特定的控件就有它自己的DataContext了。
wpf中数据绑定和INotifyPeropertyChanged的理解的更多相关文章
- wpf中数据绑定(Datacontext)的应用
在winform开发中,我们常用到ado.net进行数据绑定,在编程技术日新月异的今天,这种繁杂的数据绑定方式已不能再适合开发人员,于是微软推出了wpf,更炫的界面美化,更简洁地编写控件,在wpf中使 ...
- WPF中数据绑定问题
在数据库中字段不区分大小写,可以页面是区分的,一开始以为不区分,可我从数据库查出了数据在前台就是不显示想了半天才发现的. <sdk:DataGrid FrozenColumnCount =&qu ...
- 对WPF中MeasureOverride 和ArrangeOverride 浅理解
以前对MeasureOverride 和ArrangeOverride十分费解,看到了这篇博文茅塞顿开~ public class CustomControl1 : Panel { /// <s ...
- WPF中的数据绑定(初级)
关于WPF中的数据绑定,初步探讨 数据绑定属于WPF中比较核心的范畴,以下是对WPF中数据绑定的一个初步探讨.个人感觉还是带有问题性质的叙述比较高效,也比较容易懂 第一,什么是数据绑定? 假定有这么一 ...
- WPF中 ItemsSource 和DataContext不同点
此段为原文翻译而来,原文地址 WPF 中 数据绑定 ItemSource和 DataContext的不同点: 1.DataContext 一般是一个非集合性质的对象,而ItemSource 更期望数据 ...
- MVVM模式和在WPF中的实现(二)数据绑定
MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- WPF教程九:理解WPF中的对象资源
在WPF中,所有继承自FrameworkElement的元素都包含一个Resources属性,这个属性就是我们这篇要讲的资源. 这一篇讲解的资源是不是上一篇的程序集资源(那个是在编译过程中打包到程序集 ...
- WPF入门教程系列十八——WPF中的数据绑定(四)
六.排序 如果想以特定的方式对数据进行排序,可以绑定到 CollectionViewSource,而不是直接绑定到 ObjectDataProvider.CollectionViewSource 则会 ...
- WPF入门教程系列十五——WPF中的数据绑定(一)
使用Windows Presentation Foundation (WPF) 可以很方便的设计出强大的用户界面,同时 WPF提供了数据绑定功能.WPF的数据绑定跟Winform与ASP.NET中的数 ...
随机推荐
- expect安装
expect是在tcl基础上创建起来的,因此在安装expect之前需要安装tcl 安装TCL下载地址:http://www.tcl.tk/software/tcltk/download.html[ro ...
- SQL Server Download
{ https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads }
- net.sf.jsqlparser.statement.select.PlainSelect.getGroupBy()Lnet/sf/jsqlparse
添加pom依赖 <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>js ...
- day23_1-re模块之转义字符、分组、方法
#!/usr/bin/env python# -*- coding:utf-8 -*-# ------------------------------------------------------- ...
- mavlink 笔记1
Packet Anatomy This is the anatomy of one packet. It is inspired by the CAN and SAE AS-4 standards. ...
- HDU-1501-Zipper-字符串的dfs
Given three strings, you are to determine whether the third string can be formed by combining the ch ...
- 腾讯bugly接入插件(CocosCreator)
下载: plugin-bugly.zip (1.4 MB) 插件开源地址: https://github.com/tidys/CocosCreatorPlugins/tree/master/packa ...
- JS规则 给变量取个名字(变量命名) 必须以字母、下划线或美元符号开头;区分大小写;不允许使用JS关键字或保留字
给变量取个名字(变量命名) 我们为了区分盒子,可以用BOX1,BOX2等名称代表不同盒子,BOX1就是盒子的名字(也就是变量的名字). 我们赶快给变量取个好名字吧!变量名字可以任意取,只不过取名字要遵 ...
- 【学术篇】luogu1558&&poj2777 色板游戏
题目の传送门: luogu:https://www.luogu.org/problem/show?pid=1558 poj:http://poj.org/problem?id=2777 题目大意:给有 ...
- JavaScript变量名与函数名的命名规范
JavaScrip变量名与函数名的命名规范严格遵循以下5条: (1)首字符必须是字母.下划线.$,后跟任意的字母.数字.下划线.$ (2)严格区分大小写 (3)不能使用系统的关键字和保留字 (4)命名 ...