原创:转载请注明出处。

先说数据绑定:

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. Windows7 打开word2003提示:向程序发送命令时出现错误 解决方案

    1.关闭所有打开的Word文档:(包括任务管理器里的进程)2.复制这条命令:%appdata%\microsoft\templates3.开始 → 运行 → 粘贴上面复制的命令 → 确定4.在打开的目 ...

  2. RDD运行原理

  3. YARN设计思路

  4. JQuery ajax提交表单及表单验证

    JQuery ajax提交表单及表单验证 博客分类: jsp/html/javascript/ajax/development Kit 开源项目   注:经过验证,formValidator只适合一个 ...

  5. 函数开始处的MOV EDI, EDI的作用

    调试程序调试到系统库函数的代码时,总会发现系统函数都是从一条MOV EDI, EDI指令开始的,紧接着这条指令下面才是标准的建立函数局部栈的代码.对系统DLL比如ntdll.dll进行反汇编,可以发现 ...

  6. VS2010-MFC(常用控件:编辑框Edit Control)

    转自:http://www.jizhuomi.com/software/181.html 编辑框(Edit Control)是一种很常用的控件,我们可以在编辑框中输入并编辑文本.在前面加法计算器的例子 ...

  7. (转) Mac下面的SecureCRT(附破解方案) 更新到最新的7.3.7

    Mac下面的SecureCRT(附破解方案) 更新到最新的7.3.7 转自 http://blog.csdn.net/skykingf/article/details/17450561 http:// ...

  8. 第二十二篇:Spring简单定时任务

    背景:有些操作,不适合放在页面上让用户手动触发去执行,比如一些需要不断更新的数据(如统计数据)有些需要同步的数据,不需要非常实时,可以在固定的时间或者固定的频率执行同步 第一步:配置xml第二步:编写 ...

  9. uoj#370【UR #17】滑稽树上滑稽果

    题目 低智选手果然刷不动uoj 首先考虑一下构造一棵树显然是骗你玩的,按位与这个东西越做越小,挂到链的最下面显然不会劣于挂到之前的某一个点下面,所以我们只需要求一个排列使得答案最小就好了 设\(A=\ ...

  10. 如何在asp.net(C#)里用正则表达式验证手机号码