在WPF中进行数据绑定的时候常常会用到INotifyPropertyChanged接口来进行实现,下面来看一个INotifyPropertyChanged的案例。

下面定义一个Person类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel;
  6. namespace WpfApp
  7. {
  8. public class Person:INotifyPropertyChanged
  9. {
  10. private String _name = "张三";
  11. private int _age = 24;
  12. private String _hobby = "篮球";
  13. public String Name
  14. {
  15. set
  16. {
  17. _name = value;
  18. if (PropertyChanged != null)//有改变
  19. {
  20. PropertyChanged(this, new PropertyChangedEventArgs("Name"));//对Name进行监听
  21. }
  22. }
  23. get
  24. {
  25. return _name;
  26. }
  27. }
  28. public int Age
  29. {
  30. set
  31. {
  32. _age = value;
  33. if (PropertyChanged != null)
  34. {
  35. PropertyChanged(this, new PropertyChangedEventArgs("Age"));//对Age进行监听
  36. }
  37. }
  38. get
  39. {
  40. return _age;
  41. }
  42. }
  43. public String Hobby//没有对Hobby进行监听
  44. {
  45. get { return _hobby; }
  46. set { _hobby = value; }
  47. }
  48. public event PropertyChangedEventHandler PropertyChanged;
  49. }
  50. }

上面定义的这个Person类中,对Name和Age属性进行了监听,但是没有对Hobby进行监听。

MainWindow.xmal界面文件定义的内容如下:

  1. <Window x:Class="WpfApp.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="300" Width="350">
  5. <Grid Name="grid">
  6. <TextBox Height="20" Text="{Binding Path=Name}"  HorizontalAlignment="Left" Margin="63,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="139" />
  7. <TextBox Height="20"  Text="{Binding Path=Age}"  HorizontalAlignment="Left" Margin="63,48,0,0" Name="textBox2" VerticalAlignment="Top" Width="139" />
  8. <TextBox Height="20" Text="{Binding Path=Hobby}"  HorizontalAlignment="Left" Margin="63,82,0,0" Name="textBox3" VerticalAlignment="Top" Width="139" />
  9. <Button Content="显示用户信息" Height="26" HorizontalAlignment="Left" Margin="60,118,0,0" Name="button1" VerticalAlignment="Top" Width="144" Click="button1_Click" />
  10. <Button Content="修改用户信息" Height="26" HorizontalAlignment="Left" Margin="60,158,0,0" Name="button2" VerticalAlignment="Top" Width="144" Click="button2_Click" />
  11. <TextBlock Height="40" HorizontalAlignment="Left" Margin="13,201,0,0" Name="textBlock1"   Text="{Binding Path=Name}"  VerticalAlignment="Top" Width="88" />
  12. <TextBlock Height="40" HorizontalAlignment="Left" Margin="118,201,0,0" Name="textBlock2" Text="{Binding Path=Age}" VerticalAlignment="Top" Width="88" />
  13. <TextBlock Height="40" HorizontalAlignment="Left" Margin="222,201,0,0" Name="textBlock3" Text="{Binding Path=Hobby, Mode=TwoWay}" VerticalAlignment="Top" Width="88" />
  14. </Grid>
  15. </Window>


后台代码是:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. namespace WpfApp
  15. {
  16. /// <summary>
  17. /// MainWindow.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class MainWindow : Window
  20. {
  21. public MainWindow()
  22. {
  23. InitializeComponent();
  24. }
  25. private Person p1 = new Person();
  26. private void button1_Click(object sender, RoutedEventArgs e)
  27. {
  28. grid.DataContext = p1;//绑定数据
  29. p1.Name = "李四";
  1. p1.Hobby = "足球";
  2. }
  3. private void button2_Click(object sender, RoutedEventArgs e)
  4. {
  5. p1.Age = p1.Age + 1;
  6. p1.Hobby = "足球";
  7. }
  8. }
  9. }

当点击显示用户数据的时候

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

由于在Person中没有对Hobby进行监听,所以p1.Hobby="足球"这个语句没有起到作用。 点击修改用户信息的时候也是不能修改绑定到界面上的对应Hobby的信息(即使是在界面处写了Mode=TwoWay,也是不能进行绑定的)。

所以使用INotifyPropertyChanged的时候,需要对要进行绑定的属性进行显示的设置的,否则绑定的时候是不能进行双向绑定的,即绑定是无效的。

最新文章

  1. 虚拟机出现“操作文件.PhysicalDrive1失败”的解决方法
  2. ThoughtWorks.QRCode生成二维码
  3. 从零开始写一个Tomcat(贰)--建立动态服务器
  4. cursor属性
  5. Elasticsearch基础教程
  6. Axis2 webservice入门--Webservice的发布与调用
  7. Spring AOP就是这么简单啦
  8. python中报错&quot;json.decoder.JSONDecodeError: Expecting value:&quot;的解决
  9. GO语言从入门到放弃目录
  10. WPF 开源项目
  11. windows server 2012 流媒体服务器搭建(直播与点播)
  12. hdu5335(bfs,贪心)
  13. 《Linux内核分析》第五周学习总结 扒开系统调用的三层皮(下)
  14. .NET 中什么样的类是可使用 await 异步等待的?
  15. 网络基础&#160;Windows&#160;telnet使用简介及相关问题解决方案
  16. python3登录网页(163邮箱)实例
  17. HDUOJ-----1066Last non-zero Digit in N!
  18. Unity AssetBundle工作流
  19. JVM-内存回收算法--复制算法
  20. pdf.js 在线阅读PDF

热门文章

  1. Struts2完全解耦和
  2. 【38.24%】【POJ 1201】Intervals
  3. 【C++竞赛 E】xxx和yyy的旅行
  4. Linux下停Tomcat服务器,出现Connection refused错误解决办法
  5. Drupal 7 模块开发 建立模块帮助信息(hook_help)
  6. [RxJS] Use RxJS concatMap to map and concat high order observables
  7. expdp之后scp的惊险时刻
  8. gcc for Windows 开发环境介绍
  9. NOIP模拟 - 莫队
  10. 前端css实现最基本的时间轴