【转载】wpf数据绑定binding与INotifyPropertyChanged
WPF数据绑定号称是:数据变化会通过界面更新数据,这对新手而言,绝对是个误区,在我听说这句话的时候,我真是高兴,用了以后才发现其实没有那么美。要实现前面号称的特性,需要三个条件:1、进行绑定,2、绑定的来源要实现INotifyPropertyChanged接口,意思是,源改变了要去通知目标。3、目标要是依赖属性。
下面简单贴个例子。
XAML文件:
1
2 3 4 5 6 7 8 9 10 11 |
<Window x:Class="InotifyChangedTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local ="clr-namespace:InotifyChangedTest" Title="Window1" Height="300" Width="300"> <Grid> <TextBox Height="23" Margin="40,43,52,0" Name="textBox1" VerticalAlignment="Top" Text="{Binding Path=name}"/> <TextBox Height="23" Margin="40,94,52,0" Name="textBox2" VerticalAlignment="Top" Text="{Binding Path=desc}"/> <Button Height="23" Margin="106,0,97,42" Name="button1" VerticalAlignment="Bottom" Click="button1_Click">Button</Button> </Grid> </Window> |
TextBox 的Text是依赖属性。
cs文件,新手问题一般都比较多,我就把所有代码都复制过来了:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
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; using System.ComponentModel; namespace InotifyChangedTest private void button1_Click(object sender, RoutedEventArgs e) } |
前面的数源是在代码里通过this.DataContext = ps;这句话绑定的。接下来,贴两段代码,用XAML来替代这句话。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Window x:Class="InotifyChangedTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local ="clr-namespace:InotifyChangedTest" Title="Window1" Height="300" Width="300"> <Window.Resources> <local:personContainer x:Key="myDataSource" /> </Window.Resources> <Grid> <TextBox Height="23" Margin="40,43,52,0" Name="textBox1" VerticalAlignment="Top" Text="{Binding Path=ps.name,Source={StaticResource myDataSource}}"/> <TextBox Height="23" Margin="40,94,52,0" Name="textBox2" VerticalAlignment="Top" Text="{Binding Path=ps.desc,Source={StaticResource myDataSource}}"/> <Button Height="23" Margin="106,0,97,42" Name="button1" VerticalAlignment="Bottom" Click="button1_Click">Button</Button> </Grid> </Window> |
注意上面,定义了一个资源,程序的编译的时候会自动创建这个资源的实例。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
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; using System.ComponentModel; namespace InotifyChangedTest } |
注意到前后两个CS文件,在后面的CS里新建了一个类用来绑定,你会不会奇怪,为什么不像第一个例子一样,直接用WINDOW1里的PS呢,原因还是前面提到的,XAML里会对资源创建实例,所以如用把WINDOW1做为资源,XAML就会去创建WINDOW1的实例,CS也创建WINDOW,两个窗口冲突,编译不能通过。
【转载】wpf数据绑定binding与INotifyPropertyChanged相关推荐
- WPF数据绑定(1-简单数据绑定)
2019独角兽企业重金招聘Python工程师标准>>> 附WPF书籍推荐: 1.不要为书籍经销商的炒作蒙蔽,国内翻译的<WPF揭秘>被褒扬成经典,其实我认为该书浅尝则止, ...
- WPF and Silverlight 学习笔记(二十):WPF数据绑定概述
WPF数据绑定为应用程序提供了一种表示数据和与数据交互的简单而又一致的方法.元素能够以公共语言运行库 (CLR) 对象和 XML 的形式绑定到各种数据源中的数据. 一.数据绑定的基本概念: 数据绑定涉 ...
- WPF之Binding(转)
WPF之Binding Binding即绑定,听上去像一个音译词,很有趣,我们生活中常用的类似还有一个词,叫want to,我们叫妄图,只不过略加了感情色彩,扯远了,回到正题说绑定. 感觉这个东西在W ...
- WPF的binding
深入浅出WPF之Binding的使用(一) 在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target).一般情况下,Binding源是逻辑层对 ...
- 深入浅出WPF之Binding的使用(一)
from: http://www.cnblogs.com/akwwl/p/3421005.html 在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和 ...
- WPF 关于Binding
命名空间:System.Windows.Data 作用:提供对绑定定义的高级访问,该绑定连接绑定目标对象(通常为 WPF 元素)的属性和任何数据源(例如数据库.XML 文件,或包含数据的任何对象).在 ...
- WPF入门第六篇 WPF的Binding
WPF的Binding 在传统的Windows软件中,大部分都是UI驱动程序的模式,也可以说事件驱动程序.WPF作为Winform的升级,它把UI驱动程序彻底改变了,核心回到了数据驱动程序的模式上面, ...
- WPF中Binding使用StringFormat格式化字符串方法
原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...
- Wpf 数据绑定简介、实例1
简介: 1.WPF绑定使用的源属性必须是依赖项属性,这是因为依赖项属性具有内置的更改通知支持,元素绑定表达式使用了Xaml扩展标记, WPF绑定一个控件是使用Binding.ElementName, ...
最新文章
- 向Relay添加算子
- 2014搜狗前端面经【B事业部】
- mysql防止误删除_mysql误删除处理方法
- Hadoop2之NameNode HA详解
- bat执行clsss
- 禾匠榜店小程序商城V4独立版V4.0.25 前端+后端
- 初识编码 gbk unicode utf-8
- Maven学习总结(22)——Maven常用插件介绍
- 淘宝京东拼多多淘客源码,三合一淘客php源码cms搭建教程
- stylus 设置全局样式_vue 公共样式处理_全局styl文件
- 怎样设置电脑桌面共享计算机,局域网共享设置,小编教你电脑怎么设置局域网共享...
- 游戏对战平台编写流程研究
- Samba安装,你可能没有权限使用网络资源。请与这台服务器的管理员联系。。。。。。
- mov格式的视频转换mp4?视频格式转换这样做
- MQTT Essentials(Basics(1-5) And Features(6-10))
- 据说优秀的程序员都是这样送新年祝福的?
- 41岁职场中年人深度劝告:一定要从小公司往大公司走
- DPU网络开发SDK——DPDK(二)
- Vista上StarForce驱动的卸载
- 欧洲监管机构与银行合作开发区块链权证发行系统