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

相关推荐
通信小呆呆16 天前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?
人工智能·学习·算法·机器学习·机器人
H__Rick16 天前
自动对焦学习-3
人工智能·学习·计算机视觉
Daisy Lee16 天前
量化学习-第1章-什么是量化金融
学习·金融·datawhale
Alsn8616 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker
YM52e16 天前
买菜计算器小应用 - HarmonyOS ArkUI 开发实战-PC版本
学习·华为·harmonyos·鸿蒙·鸿蒙系统
小雨下雨的雨16 天前
HarmonyOS ArkUI训练营入门-组件掌握系列-Animation 动画效果实现-PC版本
学习·华为·harmonyos·鸿蒙
cqbzcsq16 天前
CellFlow虚拟细胞论文阅读
论文阅读·人工智能·笔记·学习·生物信息
YangYang9YangYan16 天前
2026初入职场学习数据分析的价值
学习·数据挖掘·数据分析
guslegend16 天前
理论学习:什么是 Coding Agent?
学习
自传.16 天前
尚硅谷 Vibe Coding|第三章(1) Claude Code深度使用与进阶技巧 学习笔记
笔记·学习·尚硅谷·vibecoding