WPF-INotifyPropertyChanged

    接上一篇……

    (在MVVM架构中,界面View的数据源来自ViewModel,如果想实现 从ViewModel到View 和 从View到ViewModel的双向交互,这里有一种基于MVVM框架下的方式,即使用RaisePropertyChanged,当然还有一种基于INotifyPropertyChanged接口的方式实现,这里我们主要讲下RaisePropertyChanged 实现双向交互的方式。----姜彦20180117)

     这一篇目讲INotifyPropertyChanged。

     有时候,我们需要监听属性值的变化,当属性值发生改变时,需要被通知,没有改变时不需要通知,这个时候可以用INotifyPropertyChanged来做,也可以自定义委托事件,也可以实现,方法多多,在这里只介绍INotifyPropertyChanged的方法,目的是当属性值发生变化时,直接触发,不需要再写代码。

 

     它的作用:向客户端发出某一属性值已更改的通知。

     当属性改变时,它可以通知客户端,并进行界面数据更新.而我们不用写很多复杂的代码来更新界面数据,这样可以做到方法简洁而清晰,松耦合和让方法变得更通用.可用的地方太多了:例如上传进度,实时后台数据变更等地方.目前我发现winform和silverlight都支持,确实是一个强大的接口.

 

1.View(不变跟上篇一样)

<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>
View Code

2.Model(不变跟上篇一样)

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(); }
        }


    }
}
Model Code

3.ViewModel(有变化,基于INotifyPropertyChanged接口)

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; }
        }

    }
}
ViewModel Code

4.ViewController(略有变化,逻辑不变)

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; 
        }
    }
}
ViewController Code

 

posted @ 2018-01-17 19:30  <--青青子衿-->  阅读(157)  评论(0编辑  收藏  举报
// /**/ // 在页脚Html代码 引入 // function btn_donateClick() { var DivPopup = document.getElementById('Div_popup'); var DivMasklayer = document.getElementById('div_masklayer'); DivMasklayer.style.display = 'block'; DivPopup.style.display = 'block'; var h = Div_popup.clientHeight; with (Div_popup.style) { marginTop = -h / 2 + 'px'; } } function MasklayerClick() { var masklayer = document.getElementById('div_masklayer'); var divImg = document.getElementById("Div_popup"); masklayer.style.display = "none"; divImg.style.display = "none"; } setTimeout( function () { document.getElementById('div_masklayer').onclick = MasklayerClick; document.getElementById('btn_donate').onclick = btn_donateClick; var a_gzw = document.getElementById("guanzhuwo"); a_gzw.href = "javascript:void(0);"; $("#guanzhuwo").attr("onclick","follow('33513f9f-ba13-e011-ac81-842b2b196315');"); }, 900);