• MVVM模式中WPF数据的完全绑定


    一:截图,描述:将后台代码的姓名、年龄绑定到文本框,单击”增加年龄“--年龄自+1,单击”显示年龄“--弹出年龄的显示对话框,实现了从文本框修改年龄和后台更改年龄并显示到文本框

    运行结果和解决方案管理截图如下:

    二:person类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    
    namespace 完全数据绑定
    {
        class Person : INotifyPropertyChanged//INotifyPropertyChanged是.net内置的接口,数据绑定会检测DataContext是否实现了INotifyPropertyChanged,如果实现了就会监听PropertyChanged这个属性改变的事件
        {
            private string name;//定义名字
            private int age;//定义年龄
    
            public string Name
            { get; set; }
            public int Age//定义年龄属性
            {
                get { return age;}
                set
                { 
                    this.age = value;
    
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this,new PropertyChangedEventArgs("Age"));//触发事件,参数说明:触发对象,事件数据
                    }
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;//实现接口,定义一个属性改变事件
        }
    }

    三:界面后台代码

    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;
    
    namespace 完全数据绑定
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            private Person p1 = new Person();//定义一个字段并初始化
            public MainWindow()
            {
                InitializeComponent();
            }
            
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                p1.Name = "红马車";
                p1.Age = 24;
    
                txtName.DataContext = p1;
                txtAge.DataContext = p1;
    
            }
            //增加年龄
            private void btnAdd_Click(object sender, RoutedEventArgs e)
            {
                p1.Age++;
            }
            //显示年龄
            private void btnShow_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show(p1.Age.ToString());
            } 
        }
    }

    四:什么是MVVM?

    可以说MVVM是专为WPF打造的模式, 也可以说MVVM仅仅是MVC的一个变种, 但无论如何, 就实践而言, 如果你或你的团队没有使用"Binding"的习惯, 那么研究MVVM就没有多大意义.

  • 相关阅读:
    Educational Codeforces Round 89 (Rated for Div. 2) B. Shuffle(数学/双指针)
    Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords(贪心/数学)
    Codeforces Round #648 (Div. 2) E. Maximum Subsequence Value(鸽巢原理)
    Codeforces Round #648 (Div. 2) D. Solve The Maze
    Codeforces Round #648 (Div. 2) C. Rotation Matching
    Codeforces Round #648 (Div. 2) B. Trouble Sort
    Codeforces Round #648 (Div. 2) A. Matrix Game
    一些常见的重置密码漏洞分析整理
    有无轻量级的可以显示md大纲的编辑器?
    Python3+ssl实现加密通信
  • 原文地址:https://www.cnblogs.com/hongmaju/p/3926728.html
Copyright © 2020-2023  润新知