C# INotifyPropertyChanged使用方法

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

上面定义的这个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.           
  10.         <Button Content="显示用户信息" Height="26" HorizontalAlignment="Left" Margin="60,118,0,0" Name="button1" VerticalAlignment="Top" Width="144" Click="button1_Click" />  
  11.         <Button Content="修改用户信息" Height="26" HorizontalAlignment="Left" Margin="60,158,0,0" Name="button2" VerticalAlignment="Top" Width="144" Click="button2_Click" />  
  12.   
  13.         <TextBlock Height="40" HorizontalAlignment="Left" Margin="13,201,0,0" Name="textBlock1"   Text="{Binding Path=Name}"  VerticalAlignment="Top" Width="88" />  
  14.         <TextBlock Height="40" HorizontalAlignment="Left" Margin="118,201,0,0" Name="textBlock2" Text="{Binding Path=Age}" VerticalAlignment="Top" Width="88" />  
  15.         <TextBlock Height="40" HorizontalAlignment="Left" Margin="222,201,0,0" Name="textBlock3" Text="{Binding Path=Hobby, Mode=TwoWay}" VerticalAlignment="Top" Width="88" />  
  16.     </Grid>  
  17. </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.   
  15. namespace WpfApp  
  16. {  
  17.     /// <summary>  
  18.     /// MainWindow.xaml 的交互逻辑  
  19.     /// </summary>  
  20.     public partial class MainWindow : Window  
  21.     {  
  22.         public MainWindow()  
  23.         {  
  24.             InitializeComponent();  
  25.         }  
  26.   
  27.         private Person p1 = new Person();  
  28.         private void button1_Click(object sender, RoutedEventArgs e)  
  29.         {  
  30.             grid.DataContext = p1;//绑定数据  
  31.             p1.Name = "李四";   
  32.             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的时候,需要对要进行绑定的属性进行显示的设置的,否则绑定的时候是不能进行双向绑定的,即绑定是无效的。

转载于:https://www.cnblogs.com/wlming/p/5574778.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值