原创:转载请注明出处。

先说数据绑定:

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的理解的更多相关文章

  1. wpf中数据绑定(Datacontext)的应用

    在winform开发中,我们常用到ado.net进行数据绑定,在编程技术日新月异的今天,这种繁杂的数据绑定方式已不能再适合开发人员,于是微软推出了wpf,更炫的界面美化,更简洁地编写控件,在wpf中使 ...

  2. WPF中数据绑定问题

    在数据库中字段不区分大小写,可以页面是区分的,一开始以为不区分,可我从数据库查出了数据在前台就是不显示想了半天才发现的. <sdk:DataGrid FrozenColumnCount =&qu ...

  3. 对WPF中MeasureOverride 和ArrangeOverride 浅理解

    以前对MeasureOverride 和ArrangeOverride十分费解,看到了这篇博文茅塞顿开~ public class CustomControl1 : Panel { /// <s ...

  4. WPF中的数据绑定(初级)

    关于WPF中的数据绑定,初步探讨 数据绑定属于WPF中比较核心的范畴,以下是对WPF中数据绑定的一个初步探讨.个人感觉还是带有问题性质的叙述比较高效,也比较容易懂 第一,什么是数据绑定? 假定有这么一 ...

  5. WPF中 ItemsSource 和DataContext不同点

    此段为原文翻译而来,原文地址 WPF 中 数据绑定 ItemSource和 DataContext的不同点: 1.DataContext 一般是一个非集合性质的对象,而ItemSource 更期望数据 ...

  6. MVVM模式和在WPF中的实现(二)数据绑定

    MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  7. WPF教程九:理解WPF中的对象资源

    在WPF中,所有继承自FrameworkElement的元素都包含一个Resources属性,这个属性就是我们这篇要讲的资源. 这一篇讲解的资源是不是上一篇的程序集资源(那个是在编译过程中打包到程序集 ...

  8. WPF入门教程系列十八——WPF中的数据绑定(四)

    六.排序 如果想以特定的方式对数据进行排序,可以绑定到 CollectionViewSource,而不是直接绑定到 ObjectDataProvider.CollectionViewSource 则会 ...

  9. WPF入门教程系列十五——WPF中的数据绑定(一)

    使用Windows Presentation Foundation (WPF) 可以很方便的设计出强大的用户界面,同时 WPF提供了数据绑定功能.WPF的数据绑定跟Winform与ASP.NET中的数 ...

随机推荐

  1. Mysql优化-概述

    摘抄并用于自查笔记 为什么要优化 一个应用吞吐量瓶颈往往出现在数据库的处理速度上. 随着应用程序的使用,数据库数据逐渐增多,数据库处理压力逐渐增大. 关系型数据库的数据是存放在磁盘上,读写速度慢(与内 ...

  2. linux zip,tar压缩文件夹 忽略 .git 文件夾

    linux zip 忽略 .git 文件夾 # zip 命令 zip -r bitvolution.zip bitvolution -x *.git* # tar命令压缩文件夹忽略 .git文件夹 t ...

  3. CSIC_716_20191104【流程控制语句】

    流程控制语句 if 语法结构 if 逻辑判断为真 : xxxxxx else: xxxxx while 语法结构  (continue.break) while 逻辑判断为真: xxxxxxx con ...

  4. css样式初始化代码总结

    编写css样式之前需要初始化css样式,总结如下: /* CSS Document */ html, body, div, span, object, iframe,h1, h2, h3, h4, h ...

  5. C/C++ 字符、字符串转十六进制(支持中文字符串转换)

    #include <string> // std::string #include <sstream> // std::stringstream /** * #purpose ...

  6. dos中文显示乱码怎么办?

    其实只需要一条命令 chcp 65001 执行该操作后,代码页就被变成UTF-8了 也可是GBK,  命令式:  chcp  936 2.修改窗口属性,改变字体 在命令行标题栏上点击右键,选择&quo ...

  7. csps模拟87888990部分题解

    题面:https://www.cnblogs.com/Juve/articles/11752338.html https://www.cnblogs.com/Juve/articles/1175241 ...

  8. C/C++实现单向循环链表(尾指针,带头尾节点)

    C语言实现单向循环链表,主要功能为空链表创建,链表初始化(头插法,尾插法),链表元素读取,按位置插入,(有序链表)按值插入,按位置删除,按值删除,清空链表,销毁链表. 单向循环链表和单向链表的区别:( ...

  9. axios HTTP 400后,error没有详细信息

    参考网址:axios怎么获取到error中的状态值,具体信息 error.response

  10. idae for mac部分背景色修改收集

    文章目录 所有字体默认颜色 终端背景色 行数line number背景色 line number颜色 编码区背景色 光标所在行背景色 未被使用的变量.方法或者类 控制台相关 选中文字的背景色 选中和未 ...