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 }