WPF:向客户端发出某一属性值已更改的通知INotifyPropertyChanged接口
Person.cs
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
using System.ComponentModel;namespace _01_INotifyPropertyChanged{ class Person:INotifyPropertyChanged { private double height; private double weight; public double Height { set { this.height = value; } get { return height; } } public double Weight { set { this.weight = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Weight")); } } get { return weight; } } public event PropertyChangedEventHandler PropertyChanged; }} |
MainWindow.xaml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<window x:class="_01_INotifyPropertyChanged.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="MainWindow" height="350" width="525" windowstartuplocation="CenterScreen" fontsize="14" loaded="Window_Loaded"> <grid> <grid> <grid.columndefinitions> <columndefinition></columndefinition> <columndefinition></columndefinition> <columndefinition></columndefinition> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition></rowdefinition> <rowdefinition></rowdefinition> <rowdefinition></rowdefinition> </grid.rowdefinitions> <textblock text="身高" verticalalignment="Center" horizontalalignment="Right"></textblock> <textbox text="{Binding Height}" name="txtHeight" grid.column="1" verticalalignment="Center" horizontalalignment="Center" width="58"></textbox> <button name="btnHeightIncrease" content="身高加1" grid.column="2" verticalalignment="Center" horizontalalignment="Left" click="btnHeightIncrease_Click"></button> <textblock text="体重" grid.row="1" verticalalignment="Center" horizontalalignment="Right"></textblock> <textbox text="{Binding Weight}" name="txtWeight" grid.row="1" grid.column="1" verticalalignment="Center" horizontalalignment="Center" width="58"></textbox> <button name="btnWeightIncrease" content="体重加1" grid.row="1" grid.column="2" verticalalignment="Center" horizontalalignment="Left" click="btnWeightIncrease_Click"></button> <datagrid name="dgPerson" grid.row="2" grid.columnspan="3" borderbrush="White" background="White" canuseraddrows="False"></datagrid> </grid> </grid></window> |
MainWindow.xaml.cs
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
using System.Collections.Generic;using System.Windows;using System.Windows.Documents;namespace _01_INotifyPropertyChanged{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { Person person = new Person(); public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { List<person> listPerson = new List<person>(); person.Height = 175.3; person.Weight = 66.7; listPerson.Add(person); txtHeight.DataContext = txtWeight.DataContext = person; dgPerson.ItemsSource = listPerson; } private void btnHeightIncrease_Click(object sender, RoutedEventArgs e) { person.Height++; MessageBox.Show(string.Format("身高现在是{0},但dgPerson绑定的数据没有刷新。", person.Height)); } private void btnWeightIncrease_Click(object sender, RoutedEventArgs e) { person.Weight++; MessageBox.Show(string.Format("体重现在是{0},dgPerson已刷新。", person.Weight)); } }}</person></person> |


浙公网安备 33010602011771号