1. 程式人生 > >WPFS資料繫結(要是後臺類物件的屬性值發生改變,通知在“客戶端介面與之繫結的控制元件值”也發生改變需要實現INotitypropertyChanged介面)

WPFS資料繫結(要是後臺類物件的屬性值發生改變,通知在“客戶端介面與之繫結的控制元件值”也發生改變需要實現INotitypropertyChanged介面)

WPFS資料繫結(要是後臺類物件的屬性值發生改變,通知在“客戶端介面與之繫結的控制元件值”也發生改變需要實現INotitypropertyChanged介面)

MainWindow.xaml

 1 <Window x:Class="WpfApplication1.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="350" Width="525" Loaded="Window_Loaded"> 5 <Grid> 6 <TextBox Text="{Binding Name}" Height="23" HorizontalAlignment="Left" Margin="122,68,0,0" Name="txtName" VerticalAlignment="Top" Width="120" /> 7 <TextBox Text="{Binding Age}" Height="23" HorizontalAlignment="
Left" Margin="122,124,0,0" Name="txtAge" VerticalAlignment="Top" Width="120" /> 8 <TextBlock Height="23" HorizontalAlignment="Left" Margin="82,71,0,0" Name="textBlock1" Text="姓名" VerticalAlignment="Top" /> 9 <TextBlock Height="23" HorizontalAlignment="Left" Margin="82,127,0,0" Name="
textBlock2" Text="年齡" VerticalAlignment="Top" /> 10 <Button Content="Age++" Height="23" HorizontalAlignment="Left" Margin="262,71,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 11 <Button Content="顯示Age" Height="23" HorizontalAlignment="Left" Margin="262,124,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" /> 12 </Grid> 13 </Window>

MainWindow.xaml.cs

 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 WpfApplication1
16 {
17     /// <summary>
18     /// MainWindow.xaml 的互動邏輯
19     /// </summary>
20     public partial class MainWindow : Window
21     {
22         Person p1 = new Person();
23         public MainWindow()
24         {
25             InitializeComponent();
26         }
27  
28         private void Window_Loaded(object sender, RoutedEventArgs e)
29         {
30             p1.Name = "李大釗";
31             p1.Age = 28;
32  
33             txtName.DataContext = p1;
34             txtAge.DataContext = p1;
35         }
36  
37         private void button1_Click(object sender, RoutedEventArgs e)
38         {
39             p1.Age++;
40         }
41  
42         private void button2_Click(object sender, RoutedEventArgs e)
43         {
44             MessageBox.Show((p1.Age).ToString());
45         }
46     }
47 }

Model

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.ComponentModel;
 6  
 7 namespace WpfApplication1
 8 {
 9     /// <summary>
10     /// INotifyPropertyChanged介面是向客戶端發出某一屬性值已經更改的通知
11     /// INotifyPropertyChanged是.net內建的介面,資料繫結DataContext是否實現了INotityPropertyChanged介面,如果實現了,就會監聽PropertyChanged得知屬性的變化
12     /// 如果要求後臺物件的值傳送改變,介面的值也跟著變,則需要實現INotityPropertyChanged介面。並且在物件屬性值變化後觸發事件
13     /// 如果說後臺物件的值會不變,則沒有必要實現這個介面
14     /// </summary>
15     public class Person:INotifyPropertyChanged
16     {
17         private string name;
18         public string Name
19         {
20             get
21             {
22                 return name;
23             }
24  
25             set
26             {
27                this.name = value;
28                if (PropertyChanged != null)
29                {   //如果Name屬性發生了改變,則觸發這個事件
30                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
31                }
32             }
33         }
34  
35         private int age;
36         public int Age
37         {
38             get
39             {
40                 return age;
41             }
42  
43             set
44             {
45                 this.age = value;
46                 //如果有人(資料繫結的物件來監聽的)監聽這個事件(如果有人監聽就是不等於null,如果沒人監聽這個事件就等於null)
47                 if (PropertyChanged != null)
48                 { 
49                     //如果Age屬性發生了改變,則觸發這個事件
50                     PropertyChanged(this,new PropertyChangedEventArgs("Age"));
51                 }
52             }
53         }
54  
55         public event PropertyChangedEventHandler PropertyChanged;
56         
57     }
58 }