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
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public person ps{get;set;}
        public Window1()
        {
            InitializeComponent();
            ps = new person();
            //注意这句,指定数据源
            this.DataContext = ps;
        }

private void button1_Click(object sender, RoutedEventArgs e)
        {
            ps.name += "a";
            ps.desc += "b";
        }

}
//实现INotifyPropertyChanged接口,当数据改变时通知
    public class person:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _name;
        public string name
        {
            get
            {
                return _name;
            }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    //改变时通知
                    prochanged("name");
                }
            }
        }
        private string _desc;
        public string desc
        {
            get
            {
                return _desc;
            }
            set
            {
                if (value != _desc)
                {
                    _desc = value;
                    //改变时进行通知
                    prochanged("desc");
                }
            }
        }
       
        private void prochanged(string info)
        {
            if (PropertyChanged != null)
            {
                //是不是很奇怪,这个事件发起后,处理函数在哪里?
                //我也不知道在哪里,我只知道,绑定成功后WPF会帮我们决定怎么处理。
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}

前面的数源是在代码里通过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
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public personContainer pc;
        public Window1()
        {
            InitializeComponent();
            //这里为什么不用下面注释掉的这句,而用FindResource呢?
            //如果用了注释的这句,那么程序编译的时候,会新建一个personContainer实例,xaml的资源也会新建实例
            //两个实例不相同,所以就算绑定了数据,pc改变时,界面也不能更新
            //因而需要用FindResource保证XAML和CS文件使用的是同一个实例。
            //pc = new personContainer();
            pc = (personContainer)this.FindResource("myDataSource");
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            pc.ps.name += "a";
            pc.ps.desc += "b";
        }

}
    public class personContainer
    {
        public person ps { get; set; }
        public personContainer()
        {
            ps = new person();
            ps.name = "ab";
            ps.desc = "dd";
        }
    }
    public class person:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _name;
        public string name
        {
            get
            {
                return _name;
            }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    prochanged("name");
                }
            }
        }
        private string _desc;
        public string desc
        {
            get
            {
                return _desc;
            }
            set
            {
                if (value != _desc)
                {
                    _desc = value;
                    prochanged("desc");
                }
            }
        }
        private void prochanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}

注意到前后两个CS文件,在后面的CS里新建了一个类用来绑定,你会不会奇怪,为什么不像第一个例子一样,直接用WINDOW1里的PS呢,原因还是前面提到的,XAML里会对资源创建实例,所以如用把WINDOW1做为资源,XAML就会去创建WINDOW1的实例,CS也创建WINDOW,两个窗口冲突,编译不能通过。

【转载】wpf数据绑定binding与INotifyPropertyChanged相关推荐

  1. WPF数据绑定(1-简单数据绑定)

    2019独角兽企业重金招聘Python工程师标准>>> 附WPF书籍推荐: 1.不要为书籍经销商的炒作蒙蔽,国内翻译的<WPF揭秘>被褒扬成经典,其实我认为该书浅尝则止, ...

  2. WPF and Silverlight 学习笔记(二十):WPF数据绑定概述

    WPF数据绑定为应用程序提供了一种表示数据和与数据交互的简单而又一致的方法.元素能够以公共语言运行库 (CLR) 对象和 XML 的形式绑定到各种数据源中的数据. 一.数据绑定的基本概念: 数据绑定涉 ...

  3. WPF之Binding(转)

    WPF之Binding Binding即绑定,听上去像一个音译词,很有趣,我们生活中常用的类似还有一个词,叫want to,我们叫妄图,只不过略加了感情色彩,扯远了,回到正题说绑定. 感觉这个东西在W ...

  4. WPF的binding

    深入浅出WPF之Binding的使用(一) 在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target).一般情况下,Binding源是逻辑层对 ...

  5. 深入浅出WPF之Binding的使用(一)

    from:   http://www.cnblogs.com/akwwl/p/3421005.html 在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和 ...

  6. WPF 关于Binding

    命名空间:System.Windows.Data 作用:提供对绑定定义的高级访问,该绑定连接绑定目标对象(通常为 WPF 元素)的属性和任何数据源(例如数据库.XML 文件,或包含数据的任何对象).在 ...

  7. WPF入门第六篇 WPF的Binding

    WPF的Binding 在传统的Windows软件中,大部分都是UI驱动程序的模式,也可以说事件驱动程序.WPF作为Winform的升级,它把UI驱动程序彻底改变了,核心回到了数据驱动程序的模式上面, ...

  8. WPF中Binding使用StringFormat格式化字符串方法

    原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...

  9. Wpf 数据绑定简介、实例1

    简介: 1.WPF绑定使用的源属性必须是依赖项属性,这是因为依赖项属性具有内置的更改通知支持,元素绑定表达式使用了Xaml扩展标记, WPF绑定一个控件是使用Binding.ElementName, ...

最新文章

  1. 向Relay添加算子
  2. 2014搜狗前端面经【B事业部】
  3. mysql防止误删除_mysql误删除处理方法
  4. Hadoop2之NameNode HA详解
  5. bat执行clsss
  6. 禾匠榜店小程序商城V4独立版V4.0.25 前端+后端
  7. 初识编码 gbk unicode utf-8
  8. Maven学习总结(22)——Maven常用插件介绍
  9. 淘宝京东拼多多淘客源码,三合一淘客php源码cms搭建教程
  10. stylus 设置全局样式_vue 公共样式处理_全局styl文件
  11. 怎样设置电脑桌面共享计算机,局域网共享设置,小编教你电脑怎么设置局域网共享...
  12. 游戏对战平台编写流程研究
  13. Samba安装,你可能没有权限使用网络资源。请与这台服务器的管理员联系。。。。。。
  14. mov格式的视频转换mp4?视频格式转换这样做
  15. MQTT Essentials(Basics(1-5) And Features(6-10))
  16. 据说优秀的程序员都是这样送新年祝福的?
  17. 41岁职场中年人深度劝告:一定要从小公司往大公司走
  18. DPU网络开发SDK——DPDK(二)
  19. Vista上StarForce驱动的卸载
  20. 欧洲监管机构与银行合作开发区块链权证发行系统

热门文章

  1. 紫色管理系统UI bootstrap后台模板
  2. 织梦dede企业律师事务所网模板源码
  3. 联通宣布用户存费1300元可获赠21M上网卡
  4. PHP如何实现HTML页面静态化
  5. 8 个 jQuery 的 PDF 浏览插件
  6. 14个jQuery图片放大编辑插件汇总
  7. linux(十三)之磁盘分区、创建文件系统、挂载
  8. 【AI视野·今日CV 计算机视觉论文速览 第156期】Mon, 9 Sep 2019
  9. 【Linux/Ubuntu】查询文件和文件夹大小
  10. ubuntu 下源码安装wireshark