WPF-RaisePropertyChanged

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

 

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

using GalaSoft.MvvmLight;
using System.ComponentModel;
using MVVM_Binding.Model;

namespace MVVM_Binding.ViewModel
{
    /// <summary>
    /// 交互
    /// ViewModel 为界面提供数据对象  
    /// 界面需要什么字段,就配置什么字段
    /// </summary>
    public class MainViewModel : ViewModelBase 
    {
        private Student _student;// = new Student("Jim");
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(Student stu)
        {

            this._student = stu;
        }

        /// <summary>
        /// 原始对象数据
        /// </summary>
        public Student Model
        {
            get
            {
                return this._student;
            }
        }
       
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            
            get { return this._student.Name; }
            set
            {
                if (this._student.Name != value)
                {
                    this._student.Name = value;
                    RaisePropertyChanged(() => this._student.Name);
                }

            }

        }

        /// <summary>
        /// 字符
        /// </summary>
        public string Char
        {
            get { return this._student.Char; }
            set
            {
                if (this._student.Char != value)
                {
                    this._student.Char = value;
                    RaisePropertyChanged(() => this._student.Char);
                }

            }
        }

        /// <summary>
        /// 长度
        /// </summary>
        public int Len
        {
            get { return this._student.Len; }
            set
            {
                if (this._student.Len != value)
                {
                    this._student.Len = value;
                    RaisePropertyChanged(() => this._student.Len);
                }

            }
        }

      


    }
}
ViewModel Code

4.View Controller

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"));

        public MainWindow()
        {
            InitializeComponent();           
            this.grid1.DataContext = student;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.student.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;           
        }
    }
}
Code

 

您的资助是我最大的动力!
金额随意,欢迎来赏!

我写的东西能让你能懂,那是义务

毕竟占用了你生命中的宝贵的时间和注意力

要是你还能喜欢我的作品,那就是缘分了

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

如果,想给予我更多的鼓励,求打

因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【青青子衿】!

联系或打赏博主【青青子衿】!https://home.cnblogs.com/u/jiangyan219/

posted @ 2018-01-17 19:04  <--青青子衿-->  阅读(1939)  评论(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);