WPF中INotifyPropertyChanged用法与数据绑定
在WPF中进行数据绑定的时候常常会用到INotifyPropertyChanged接口来进行实现,下面来看一个INotifyPropertyChanged的案例。
下面定义一个Person类:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ComponentModel;
- namespace WpfApp
- {
- public class Person:INotifyPropertyChanged
- {
- private String _name = "张三";
- private int _age = 24;
- private String _hobby = "篮球";
- public String Name
- {
- set
- {
- _name = value;
- if (PropertyChanged != null)//有改变
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Name"));//对Name进行监听
- }
- }
- get
- {
- return _name;
- }
- }
- public int Age
- {
- set
- {
- _age = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Age"));//对Age进行监听
- }
- }
- get
- {
- return _age;
- }
- }
- public String Hobby//没有对Hobby进行监听
- {
- get { return _hobby; }
- set { _hobby = value; }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- }
- }
上面定义的这个Person类中,对Name和Age属性进行了监听,但是没有对Hobby进行监听。
MainWindow.xmal界面文件定义的内容如下:
- <Window x:Class="WpfApp.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="300" Width="350">
- <Grid Name="grid">
- <TextBox Height="20" Text="{Binding Path=Name}" HorizontalAlignment="Left" Margin="63,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="139" />
- <TextBox Height="20" Text="{Binding Path=Age}" HorizontalAlignment="Left" Margin="63,48,0,0" Name="textBox2" VerticalAlignment="Top" Width="139" />
- <TextBox Height="20" Text="{Binding Path=Hobby}" HorizontalAlignment="Left" Margin="63,82,0,0" Name="textBox3" VerticalAlignment="Top" Width="139" />
- <Button Content="显示用户信息" Height="26" HorizontalAlignment="Left" Margin="60,118,0,0" Name="button1" VerticalAlignment="Top" Width="144" Click="button1_Click" />
- <Button Content="修改用户信息" Height="26" HorizontalAlignment="Left" Margin="60,158,0,0" Name="button2" VerticalAlignment="Top" Width="144" Click="button2_Click" />
- <TextBlock Height="40" HorizontalAlignment="Left" Margin="13,201,0,0" Name="textBlock1" Text="{Binding Path=Name}" VerticalAlignment="Top" Width="88" />
- <TextBlock Height="40" HorizontalAlignment="Left" Margin="118,201,0,0" Name="textBlock2" Text="{Binding Path=Age}" VerticalAlignment="Top" Width="88" />
- <TextBlock Height="40" HorizontalAlignment="Left" Margin="222,201,0,0" Name="textBlock3" Text="{Binding Path=Hobby, Mode=TwoWay}" VerticalAlignment="Top" Width="88" />
- </Grid>
- </Window>

后台代码是:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace WpfApp
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- private Person p1 = new Person();
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- grid.DataContext = p1;//绑定数据
- p1.Name = "李四";
- p1.Hobby = "足球";
- }
- private void button2_Click(object sender, RoutedEventArgs e)
- {
- p1.Age = p1.Age + 1;
- p1.Hobby = "足球";
- }
- }
- }
当点击显示用户数据的时候

下面看看这些信息具体都来自于哪儿?

由于在Person中没有对Hobby进行监听,所以p1.Hobby="足球"这个语句没有起到作用。 点击修改用户信息的时候也是不能修改绑定到界面上的对应Hobby的信息(即使是在界面处写了Mode=TwoWay,也是不能进行绑定的)。
所以使用INotifyPropertyChanged的时候,需要对要进行绑定的属性进行显示的设置的,否则绑定的时候是不能进行双向绑定的,即绑定是无效的。
WPF中INotifyPropertyChanged用法与数据绑定的更多相关文章
- WPF中TreeView控件数据绑定和后台动态添加数据(二)
写在前面:在(一)中,介绍了TreeView控件MVVM模式下数据绑定的方法.在这篇文章中,将总结给节点添加事件的方法,这样说有些不对,总之实现的效果就是点击某个节点,将出现对应于该节点的页面或者数据 ...
- WPF中TreeView控件数据绑定和后台动态添加数据(一)
数据绑定: 更新内容:补充在MVVM模式上的TreeView控件数据绑定的代码. xaml代码: <TreeView Name="syntaxTree" ItemsSourc ...
- wpf中INotifyPropertyChanged的用法
using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using Sy ...
- WPF中ComboBox用法
The ComboBox control is in many ways like the ListBox control, but takes up a lot less space, becaus ...
- WPF中的数据绑定!!!
引用自:https://msdn.microsoft.com/zh-cn/magazine/cc163299.aspx 数据点: WPF 中的数据绑定 数据点 WPF 中的数据绑定 John Pap ...
- MVVM模式和在WPF中的实现(二)数据绑定
MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- WPF入门教程系列十五——WPF中的数据绑定(一)
使用Windows Presentation Foundation (WPF) 可以很方便的设计出强大的用户界面,同时 WPF提供了数据绑定功能.WPF的数据绑定跟Winform与ASP.NET中的数 ...
- WPF中使用MVVM模式进行简单的数据绑定
计划慢慢整理自己在WPF学习和工作应用中的一些心得和想法,先从一个简单的用法说起 在WPF中,XAML标记语言中绑定数据,而数据源就是指定为ViewModel类,而非界面本身的逻辑代码类 这样一定程度 ...
- wpf中数据绑定(Datacontext)的应用
在winform开发中,我们常用到ado.net进行数据绑定,在编程技术日新月异的今天,这种繁杂的数据绑定方式已不能再适合开发人员,于是微软推出了wpf,更炫的界面美化,更简洁地编写控件,在wpf中使 ...
随机推荐
- js中ajax连接服务器open函数的另外两个默认参数get请求和默认异步(open的post方式send函数带参数)(post请求和get请求区别:get:快、简单 post:安全,量大,不缓存)(服务器同步和异步区别:同步:等待服务器响应当中浏览器不能做别的事情)(ajax和jquery一起用的)
js中ajax连接服务器open函数的另外两个默认参数get请求和默认异步(open的post方式send函数带参数)(post请求和get请求区别:get:快.简单 post:安全,量大,不缓存)( ...
- mysql 序列号生成器 (自定义函数)
https://yq.aliyun.com/articles/42600 http://bbs.csdn.net/topics/360203885 http://www.tuicool.com/art ...
- xp2p系统的10点技术创新和经验总结
最近在开发完善九天鸟的xp2p系统,解决了很多技术问题,特此总结下. 第一个项目开发,非常重要,它对建立开发规范.团队协作.开发效率,有很重大的意义. 1.分页前台AJAX异步分页,用咱们自己的fup ...
- 【图解】Web前端实现相似Excel的电子表格
在本文中,我将用图解的方式用Wijmo(JavaScript库)中的SpreadJS来一步一步实现网页上的电子表格产品SpreadSheet(比如可构建Office 365 Excel产品.Go ...
- 《Erlang程序设计》学习笔记-第2章 并发编程
http://blog.csdn.net/karl_max/article/details/3977860 1. 并发原语: (1) Pid = spawn(Fun) %% 创建一个新的并发进程,用于 ...
- 我的Boss有性能优化强迫症
我有一个Boss,他曾经在阿里深造,在UC修炼,在一号店奔波. 经过几个月的合作开发和技术交流,我发现他非常在乎程序的性能,但是呢,对于有些地方,我觉得划不来. 比如说, 把数据库中的30多条记录,查 ...
- 项目流程管理&&架构总结
1 项目背景 所在业务在早期没有营销费用,买家购买商品的折扣优惠是由卖家提供的.全部订单的终于价格是由卖家和业务方确定的,整个购买流程非常easy. 如今此业务收受到公司重视,业务团队能申请到营销费用 ...
- solr 7.x 查询及高亮
查询时的api分为两种一种是万能的set,还有一种是setxxxquery @Test public void search2() throws Exception{ HttpSolrClient s ...
- Android菜鸟的成长笔记(19)——Service的生命周期
前面两篇文章介绍了关于Service的两种启动方式,简要总结如下: Context.startService() Context.bindService() 1. startService()的目的是 ...
- 聊聊QPS/TPS/并发量/系统吞吐量的概念
原文:聊聊QPS/TPS/并发量/系统吞吐量的概念 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/cainiao_user/article/deta ...