INotifyPropertyChanged 接口:向客户端发出某一属性值已更改的通知。

NotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。

一般使用地方是:加载数据时,及时更新相应的数据加载名称。操作功能时,及时提示相应的错误信息。

实例:

xaml代码:

<TextBlock Margin="80,5,80,0" TextWrapping="Wrap" Foreground="White" FontFamily="微软雅黑" Name="txtInfo" Text="{Binding Message, Mode=TwoWay}" ToolTip="{Binding Message}" TextTrimming="WordEllipsis" Grid.Row="2" FontSize="14"></TextBlock>

后台代码:

private string _message = string.Empty;
/// <summary>
/// 错误消息
/// </summary>
public string Message
{
get { return _message; }
set
{
_message = value;
//使用时用Message才能反应到控件中,直接给_message赋值不能直接反应到控件中
NotifyPropertyChanged("Message");
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

消息赋值:

Message = "正在加载数据!";

详细实例(抄袭):

在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 = "李四";
  30. p1.Hobby = "足球";
  1. }
  2. private void button2_Click(object sender, RoutedEventArgs e)
  3. {
  4. p1.Age = p1.Age + 1;
  5. p1.Hobby = "足球";
  6. }
  7. }
  8. }

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

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

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

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

C# INotifyPropertyChanged使用方法的更多相关文章

  1. WINFROM中自定义控件之绑定数据即时更新

    相信在WINFROM中写自定义控件或者用户控件,很多人都多多少少用过点 最近发现一个用户控件,绑定的数据源没办法自动更新,其实以前处理过这类的问题,可是这次遇到又花了1个多小时,所以决定记下来 在用户 ...

  2. javaSE27天复习总结

    JAVA学习总结    2 第一天    2 1:计算机概述(了解)    2 (1)计算机    2 (2)计算机硬件    2 (3)计算机软件    2 (4)软件开发(理解)    2 (5) ...

  3. WPF 数据绑定,界面刷新的两种方法-----INotifyPropertyChanged

    .Netformwork4.0及以下版本 -------INotifyPropertyChanged 命名空间: System.ComponentModel 后台代码 public partial c ...

  4. 【C#】实现INotifyPropertyChanged的3种方法

    class StudentItemViewModel:INotifyPropertyChanged { public event PropertyChangedEventHandler Propert ...

  5. DataGridView DataSource INotifyPropertyChanged 避免闪烁的方法

    代码说话: dgvPosition就是需要避免闪烁的DataGridView 主要是加2段代码 1.SetStyle 2.datagridview设置DoubleBuffered属性为True pub ...

  6. 【Win 10应用开发】延迟加载图片的另一种方法

    上一篇文章中老周给大伙介绍了x:Phase和x:Bind的用法,并演示了一个延迟加载的示例.不过,那个例子会遗留一个问题,就是UI线程被阻塞,所以启动应用较慢. 如果希望图片可以延迟加载,或许我们可以 ...

  7. Data Binding和INotifyPropertyChanged是如何协调工作的?

    前言 WPF的一大基础就是Data Binding.在基于MVVM架构的基础上,只有通过实现INotifyPropertyChanged接口的ViewModel才能够用于Data Binding. 要 ...

  8. INotifyPropertyChanged接口的PropertyChanged 事件

    INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知. 例如,考虑一个带有名为 FirstName 属性的 Person 对象. 若要提供 ...

  9. 为Page添加INotifyPropertyChanged功能

    在Page页面里面, DataContext 更新后,前台数据要求会自动更新. 但前台的绑定如果用x:bind 语法. 它要求强类型.直接关联到DataContext上就不行了. 需要为Page 添加 ...

随机推荐

  1. springMVC和json结合传递数据

    1. 新建web project 2. 增加jar 3. 改写web.xml <?xml version="1.0" encoding="UTF-8"?& ...

  2. Cow Rectangles

    Cow Rectangles 题目描述 The locations of Farmer John's N cows (1 <= N <= 500) are described by dis ...

  3. Mysql集群读写分离(Amoeba)

    Amoeba原理戳这里:Amoeba详细介绍 实验环境 Master.Amoeba--IP:192.168.1.5 Slave---IP:192.168.1.10 安装JDK JDK下载地址:http ...

  4. Android IBinder的linkToDeath介绍

    先看官方解释: The linkToDeath() method can be used to register a IBinder.DeathRecipient with the IBinder, ...

  5. 转发:iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

    -- iOS事件全面解析 转载来自崔江涛(KenshinCui) 链接:http://www.cnblogs.com/kenshincui/p/3950646.html 概览 iPhone的成功很大一 ...

  6. $.extend 用法

    此处参考了RascallySnake 的博客 $.extend(boolean, dest, src1, src2, src3) 第一个参数boolean代表是否进行深度拷贝,其余参数和前面介绍的一致 ...

  7. ID3决策树预测的java实现

    刚才写了ID3决策树的建立,这个是通过决策树来进行预测.这里主要用到的就是XML的遍历解析,比较简单. 关于xml的解析,参考了: http://blog.csdn.net/soszou/articl ...

  8. LPC1788的EMC驱动norflash

    Norflash型号为sst39vf32 #ifndef __NORFLASH_H_ #define __NORFLASH_H_ #include "common.h" #incl ...

  9. 《C程序设计语言》读书笔记----习题1-20

    练习1-20:编写程序detab,将输入中的制表符替换成适当数目的空格,使得空格充满到下一个制表符终止位的地方,.假设制表符终止位的位置时固定的,比如每隔n列就会出现一个终止位. 这里要理解“制表符” ...

  10. iOS开发中视图控制器ViewControllers之间的数据传递

    iOS开发中视图控制器ViewControllers之间的数据传递 这里我们用一个demo来说明ios是如何在视图控制器之间传递重要的参数的.本文先从手写UI来讨论,在下一篇文章中讨论在storybo ...