WPF(1)的MVVM的数据驱动学习示例

MVVM

Model:数据模型、View 界面、ViewModel 业务逻辑处理

项目结构

界面数据绑定

复制代码
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label Content="输入的值" HorizontalAlignment="Left" Margin="68,52,0,0" VerticalAlignment="Top" Height="35" Width="69"/>

        <!--TextWrapping="Wrap" 内容是否换行-->
        <TextBox Name="txtUserName" Text="{Binding UserName}" HorizontalAlignment="Left" Margin="142,52,0,242.667" TextWrapping="Wrap" Width="123"/>
        <Button Content="SHOW" Click="Button_Click" HorizontalAlignment="Left" Margin="285,55,0,0" VerticalAlignment="Top" Width="78" Height="23"/>
        <!--           控件数据的绑定-->
        <Label Content="{Binding UserName}" HorizontalAlignment="Left" Margin="142,87,0,0" VerticalAlignment="Top"/>
    </Grid>
</Window>

后台代码

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp1.ViewModels;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private UserViewModel _userViewModel;
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _userViewModel = new UserViewModel();
            //指定界面的数据对上下文对象,XML Binding 绑定值时使用
            this.DataContext = _userViewModel;
            _userViewModel.UserName = "cjhText";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _userViewModel.UserName = txtUserName.Text;
        }
    }
}

UserViewModel

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp1.ViewModels
{
    public class UserViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _userName;

        public string UserName
        {
            set {
                _userName = value;
                //数据更新 通知界面绑定的地方更新数据
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("UserName"));
            }
            get {
                return _userName;
            }
        }
        //propfull

        private int _sex;
        public int Sex
        {
            get { return _sex; }
            set { _sex = value; }
        }

    }
}

END

相关推荐
詩不诉卿35 分钟前
zephyr学习之自定义外部module记录
学习
浮游本尊36 分钟前
React 18.x 学习计划 - 第十三天:部署与DevOps实践
学习·react.js·状态模式
wdfk_prog1 小时前
[Linux]学习笔记系列 -- [drivers][dma]dmapool
linux·笔记·学习
电饭叔1 小时前
Tkinter Button 括号内的核心参数详解
python·学习
闵帆3 小时前
反演学习器面临的鸿沟
人工智能·学习·机器学习
EnglishJun3 小时前
数据结构的学习(二)---Makefile的使用
linux·运维·学习
呱呱巨基3 小时前
c语言 文件操作
c语言·开发语言·c++·笔记·学习
嗯嗯**5 小时前
Neo4j学习1:概述、安装
学习·neo4j·概述·安装·图数据库·jdk21
Century_Dragon5 小时前
新能源汽车教学新体验:大众ID.4结构原理教学软件
学习
yangzheui5 小时前
【VUE2转VUE3学习笔记】-Day1:模板语法
vue.js·笔记·学习