当前位置: 首页 > news >正文

wpf .net 8 使用mvvm指南

xaml:

<UserControl x:Class="WpfApp1.Controls.GWEditControl.GWEditCotrol"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:local="clr-namespace:WpfApp1.Controls.GWEditControl"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Button Content="{Binding UserDemo}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDown">
                    <i:InvokeCommandAction Command="{Binding TestClickCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
    </Grid>
</UserControl>

 

ViewModel类:

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1.Controls.GWEditControl
{
    public partial class GWEditControlViewModel:ObservableObject
    {
        /// <summary>
        /// 用户备注
        /// </summary>
        [ObservableProperty]
        private string? userDemo;

        /// <summary>
        /// 绑定的事件
        /// </summary>
        [RelayCommand]
        private void TestClick()
        {
            MessageBox.Show("test");
        }
    }
}

窗口代码类(把界面和viewmodel关联起来):

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;

namespace WpfApp1.Controls.GWEditControl
{
    /// <summary>
    /// GWEditCotrol.xaml 的交互逻辑
    /// </summary>
    public partial class GWEditCotrol : UserControl
    {
        public GWEditCotrol()
        {
            InitializeComponent();
            this.DataContext = new GWEditControlViewModel();
        }
    }
}

http://www.jsqmd.com/news/8823/

相关文章:

  • office办公软件
  • 2025.10.4训练记录
  • 2025年微信小程序开发:AR/VR与电商的最新案例 - 指南
  • 2048小游戏C++板来啦! - 指南
  • 详细介绍:vue+cesium示例:3Dtiles三维模型高度调整(附源码下载)
  • st表 + 变形的djs (好题
  • 在wpf .net 8项目中使用materialDesign 4 以上版本的的注意事项
  • 李臻20242817_安全文件传输系统项目报告_第14周 - 指南
  • 洛谷P14120 题解 - lemon
  • 33 ACwing 294 Count The Repetitions 题解
  • 量化投资 —— 实践 (续)
  • 30 ACwing 291 蒙德里安的梦想 题解
  • 21 ACwing 289 环路运输 题解
  • 13 ACwing 283 Polygon 题解
  • 12 ACwing 282 石子合并 题解
  • 11 ACwing 281 Coins 题解
  • 某中心科学家荣获多项计算机技术大奖
  • 4 ACwing 274 Mobile Service 题解
  • 3 ACwing 273 Making the Grade 题解
  • 1 ACwing 271 Mr
  • 2 ACwing 272 LCIS 最长公共上升子序列 题解
  • 实用指南:【MySQL】索引特性
  • 用 Haxe 实现英文数字验证码识别
  • 出题四
  • 实用指南:B站视频下载器 v1.0.4|免登录下载1080P视频
  • 7 2025 07 15 模拟赛题解
  • 使用 OCaml 实现验证码识别
  • 资料中台(大材料平台)之数据仓库建设
  • 私有云大数据部署:从开发到生产(Docker、K8s、HDFS/Flink on K8s) - 详解
  • 差分约束模板