栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

MVVMLight使用笔记-(2)-数据绑定

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MVVMLight使用笔记-(2)-数据绑定

系列文章目录

MVVMLight使用笔记-(1)-安装及配置


前言

本文记录的是使用MVVMLight进行数据绑定的内容,大致分为属性绑定和事件绑定。通过一个记录点击按钮点击次数的案例来记录MVVMLight绑定数据的过程。

文章目录
  • 系列文章目录
  • 前言
  • 一、Model目录
  • 二、属性绑定
    • 2.1、 修改MainWindow.cs
    • 2.2、 MainViewModel.cs
    • 2.3、运行程序
  • 三、事件绑定
    • 3.1、修改
    • 3.2、修改MainWindow.cs
  • 总结

一、Model目录


在项目目录中创建Model目录,其实就是MVVM中的第一个字母M,用来负责数据实体的结构处理,与ViewModel进行交互。

MainModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;


namespace MVVMLightSolution.Model
{
    public class MainModel : ObservableObject
    {
        private int count;

        public int Count
        {
            get
            {
                return count;
            }
            set
            {
                count = value;
                RaisePropertyChanged();
            }
        }
    }
}

MainModel要继承命名控件下GalaSoft.MvvmLight的ObservableObject类,
在类中,通过RaisePropertyChanged() 方法检测字段的变化。当发生变化的时候会通过V也就是View中数据的变化。

二、属性绑定 2.1、 修改MainWindow.cs

    
        
    
    
        


在页面需要设置该页面的DataContext


    

上面的代码指明了该页面的数据来源是Locator,而这个Locator是在上一篇笔记中提到过的App.xaml中定义的。

      

而数据绑定的数据路径就是Main,Main是ViewModelLocator中的Main属性。

2.2、 MainViewModel.cs
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using MVVMLightSolution.Model;

namespace MVVMLightSolution.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        private MainModel mainModel;


        public MainModel MainModel
        {
            get { return mainModel; }
            set
            {
                mainModel = value;
                RaisePropertyChanged();
            }
        }

        /// 
        /// Initializes a new instance of the MainViewModel class.
        /// 
        public MainViewModel()
        {

            MainModel = new MainModel() { Count = 2 };
        }
        public void Add()
        {
            mainModel.Count++;
        }
    }
}
2.3、运行程序


我们运行程序即可看到数据已经展示在页面上了。

三、事件绑定

在本小节处理数据的事件绑定,在页面添加新的按钮,每点击一次在页面上修改页面的数据。

3.1、修改
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using MVVMLightSolution.Model;

namespace MVVMLightSolution.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        private MainModel mainModel;
        public MainModel MainModel
        {
            get { return mainModel; }
            set
            {
                mainModel = value;
                RaisePropertyChanged();
            }
        }

        /// 
        /// Initializes a new instance of the MainViewModel class.
        /// 
        public MainViewModel()
        {
            MainModel = new MainModel() { Count = 2 };
            AddCommand = new RelayCommand(Add);
        }
        public void Add()
        {
            mainModel.Count++;
        }
        /// 
        /// 用于完成点击事件
        /// 
        public RelayCommand AddCommand { get; set; }
    }
}
3.2、修改MainWindow.cs

    
        
    
    
        

在此文件中修改了内容如下

页面如图

最终结果


总结

以上就是数据绑定中的属性绑定和事件绑定。
若有不足,请指教。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955677.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号