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

相关推荐
狮恒25 分钟前
OpenHarmony Flutter 分布式数据管理实战:全场景数据一致性与高效流转方案
wpf
●VON34 分钟前
小V健身助手开发手记(四):打造专属健康空间——以 PersonContent构建统一风格的个人中心
人工智能·学习·openharmony·开源鸿蒙·von
●VON36 分钟前
小V健身助手开发手记(三):用成就点燃坚持——构建可视化激励系统
学习·openharmony·总结·开源鸿蒙·von
Nan_Shu_61441 分钟前
学习:Vue (2)
javascript·vue.js·学习
狮恒1 小时前
OpenHarmony Flutter 分布式音视频:跨设备流传输与实时协同交互方案
分布式·flutter·wpf·openharmony
YJlio1 小时前
桌面工具学习笔记(11.1):BgInfo——给服务器桌面“刻”上关键信息
服务器·笔记·学习
TL滕2 小时前
从0开始学算法——第十五天(滑动窗口)
笔记·学习·算法
失败才是人生常态2 小时前
并发编程场景题学习
学习
醇氧2 小时前
springAI学习 一
学习·spring·ai·ai编程
菜鸟‍2 小时前
【论文学习】Co-Seg:互提示引导的组织与细胞核分割协同学习
人工智能·学习·算法