WPF-INotifyPropertyChanged
接上一篇……
(在MVVM架构中,界面View的数据源来自ViewModel,如果想实现 从ViewModel到View 和 从View到ViewModel的双向交互,这里有一种基于MVVM框架下的方式,即使用RaisePropertyChanged,当然还有一种基于INotifyPropertyChanged接口的方式实现,这里我们主要讲下RaisePropertyChanged 实现双向交互的方式。----姜彦20180117)
这一篇目讲INotifyPropertyChanged。
有时候,我们需要监听属性值的变化,当属性值发生改变时,需要被通知,没有改变时不需要通知,这个时候可以用INotifyPropertyChanged来做,也可以自定义委托事件,也可以实现,方法多多,在这里只介绍INotifyPropertyChanged的方法,目的是当属性值发生变化时,直接触发,不需要再写代码。
它的作用:向客户端发出某一属性值已更改的通知。
当属性改变时,它可以通知客户端,并进行界面数据更新.而我们不用写很多复杂的代码来更新界面数据,这样可以做到方法简洁而清晰,松耦合和让方法变得更通用.可用的地方太多了:例如上传进度,实时后台数据变更等地方.目前我发现winform和silverlight都支持,确实是一个强大的接口.
1.View(不变跟上篇一样)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<Window x:Class="MVVM_Binding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:MVVM_Binding.ViewModel" Title="MainWindow" Height="350" Width="525"> <Grid x:Name="grid1" > <TextBox Height="23" HorizontalAlignment="Left" Margin="71,42,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding Path= Name,Mode=TwoWay}" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="71,81,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Path= Char,Mode=TwoWay}" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="71,123,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" Text="{Binding Path= Len}" /> <Button Content="测试" Height="23" HorizontalAlignment="Left" Margin="221,42,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> <Button Content="测试2" Height="23" HorizontalAlignment="Left" Margin="221,78,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" /> </Grid> </Window>
2.Model(不变跟上篇一样)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MVVM_Binding.Model { /// <summary> /// 原始数据类对象 可以不只有这三个字段 提供给ViewModel数据支持 /// </summary> public class Student { /// <summary> /// 构造函数 /// </summary> /// <param name="name"></param> public Student(string name) { this.Name = name; } /// <summary> /// 姓名 /// </summary> public string Name { get; set; } /// <summary> /// 字符 /// </summary> public string Char { get { return this.Name.Substring(2, 1); } set { Name = value; } } /// <summary> /// 长度 /// </summary> public int Len { get { return this.Name.Length; } set { Name = value.ToString(); } } } }
3.ViewModel(有变化,基于INotifyPropertyChanged接口)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using MVVM_Binding.Model; namespace MVVM_Binding.ViewModel { public class MainViewModel1 : INotifyPropertyChanged { private Student _stu; /// <summary> /// 构造函数 /// </summary> public MainViewModel1(Student stu) { this._stu = stu; } /// <summary> /// 原实体类对象模型 /// </summary> public Student Model { get { return this._stu; } } public event PropertyChangedEventHandler PropertyChanged; public string Name { get { return this._stu.Name; } set { this._stu.Name = value; //OnPropertyChanged("Name"); //PropertyChanged(this, new PropertyChangedEventArgs("Name")); if (this.PropertyChanged != null) { this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name")); } } } public string Char { get { return this._stu.Char; } set { this._stu.Char = value; } } public int Len { get { return this._stu.Len; } set { this._stu.Len = value; } } } }
4.ViewController(略有变化,逻辑不变)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; using MVVM_Binding.ViewModel; using MVVM_Binding.Model; namespace MVVM_Binding { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { //private MainViewModel student = new MainViewModel(new Student("Hello"));//ViewModelBase双向交互的模式 姜彦20180117 private MainViewModel1 student1 = new MainViewModel1(new Student("Hello")); public MainWindow() { InitializeComponent(); //this.grid1.DataContext = student; this.grid1.DataContext = student1; } private void button1_Click(object sender, RoutedEventArgs e) { //this.student.Name = "JiangYan"; this.student1.Name = "JiangYan"; } private void button2_Click(object sender, RoutedEventArgs e) { //MainViewModel stu = this.student;//你可以取消这两行的注释,修改下textbox的内容,在int i=0 这里打个断点,看看textbox更改后的数据是否传递进来了 姜彦20180117 //int i = 0; //this.student = new MainViewModel(new Student("JiangYan")); //this.grid1.DataContext = student; this.student1 = new MainViewModel1(new Student("JiangYan")); this.grid1.DataContext = student1; } } }
![](https://files-cdn.cnblogs.com/files/jiangyan219/Alipay.bmp)
您的资助是我最大的动力!
金额随意,欢迎来赏!
![](https://files-cdn.cnblogs.com/files/jiangyan219/WeChat.bmp)
我写的东西能让你能懂,那是义务
毕竟占用了你生命中的宝贵的时间和注意力
要是你还能喜欢我的作品,那就是缘分了
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【青青子衿】!