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類:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ComponentModel;
- namespace WpfApp
- {
- public class Person:INotifyPropertyChanged
- {
- private String _name = "張三";
- private int _age = 24;
- private String _hobby = "籃球";
- public String Name
- {
- set
- {
- _name = value;
- if (PropertyChanged != null)//有改變
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Name"));//對Name進行監聽
- }
- }
- get
- {
- return _name;
- }
- }
- public int Age
- {
- set
- {
- _age = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Age"));//對Age進行監聽
- }
- }
- get
- {
- return _age;
- }
- }
- public String Hobby//沒有對Hobby進行監聽
- {
- get { return _hobby; }
- set { _hobby = value; }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- }
- }
上面定義的這個Person類中,對Name和Age屬性進行了監聽,但是沒有對Hobby進行監聽。
MainWindow.xmal界面文件定義的內容如下:
- <Window x:Class="WpfApp.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="300" Width="350">
- <Grid Name="grid">
- <TextBox Height="20" Text="{Binding Path=Name}" HorizontalAlignment="Left" Margin="63,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="139" />
- <TextBox Height="20" Text="{Binding Path=Age}" HorizontalAlignment="Left" Margin="63,48,0,0" Name="textBox2" VerticalAlignment="Top" Width="139" />
- <TextBox Height="20" Text="{Binding Path=Hobby}" HorizontalAlignment="Left" Margin="63,82,0,0" Name="textBox3" VerticalAlignment="Top" Width="139" />
- <Button Content="顯示用戶信息" Height="26" HorizontalAlignment="Left" Margin="60,118,0,0" Name="button1" VerticalAlignment="Top" Width="144" Click="button1_Click" />
- <Button Content="修改用戶信息" Height="26" HorizontalAlignment="Left" Margin="60,158,0,0" Name="button2" VerticalAlignment="Top" Width="144" Click="button2_Click" />
- <TextBlock Height="40" HorizontalAlignment="Left" Margin="13,201,0,0" Name="textBlock1" Text="{Binding Path=Name}" VerticalAlignment="Top" Width="88" />
- <TextBlock Height="40" HorizontalAlignment="Left" Margin="118,201,0,0" Name="textBlock2" Text="{Binding Path=Age}" VerticalAlignment="Top" Width="88" />
- <TextBlock Height="40" HorizontalAlignment="Left" Margin="222,201,0,0" Name="textBlock3" Text="{Binding Path=Hobby, Mode=TwoWay}" VerticalAlignment="Top" Width="88" />
- </Grid>
- </Window>

后台代碼是:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace WpfApp
- {
- /// <summary>
- /// MainWindow.xaml 的交互邏輯
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- private Person p1 = new Person();
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- grid.DataContext = p1;//綁定數據
- p1.Name = "李四";
- p1.Hobby = "足球";
- }
- private void button2_Click(object sender, RoutedEventArgs e)
- {
- p1.Age = p1.Age + 1;
- p1.Hobby = "足球";
- }
- }
- }
當點擊顯示用戶數據的時候

下面看看這些信息具體都來自於哪兒?

由於在Person中沒有對Hobby進行監聽,所以p1.Hobby="足球"這個語句沒有起到作用。 點擊修改用戶信息的時候也是不能修改綁定到界面上的對應Hobby的信息(即使是在界面處寫了Mode=TwoWay,也是不能進行綁定的)。
所以使用INotifyPropertyChanged的時候,需要對要進行綁定的屬性進行顯示的設置的,否則綁定的時候是不能進行雙向綁定的,即綁定是無效的。
