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

WPF学习(6) -- WPF命令和通知

一 、WPF命令

1.ICommand代码

创建一个文件夹和文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace 学习.Command
{public class MyCommand : ICommand{Action executeAction;public MyCommand(Action action){executeAction = action;}public event EventHandler CanExecuteChanged;public bool CanExecute(object parameter){return true;}public void Execute(object parameter){executeAction();}}
}

2.view model代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using 学习.Command;namespace 学习.ViewModels
{public class MainViewModel{public MyCommand ShowCommand {  get; set; }public string Name { get; set; }public MainViewModel(){Name = "Hellow";ShowCommand = new MyCommand(Show);}public void Show(){Name = "点击了按钮";MessageBox.Show(Name);}}
}

3.xaml.cs代码

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 System.Collections.ObjectModel;
using 学习.ViewModels;namespace 学习
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new MainViewModel();}}}

4.xaml代码

<Window x:Class="学习.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:学习.ViewModels"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel><TextBox Text="{Binding Name}"/><Button Content="SHOW" Command="{Binding ShowCommand}"/></StackPanel></Grid>
</Window>

4.结果

点击按钮之后Hellow并没有改变

二、通知更改

创建Base文件放置

1.ViewModelBase代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;namespace 学习
{public class ViewModelBase : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged([CallerMemberName]string propertyName = ""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}
}

2.view model代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using 学习.Command;namespace 学习.ViewModels
{public class MainViewModel : ViewModelBase{public MyCommand ShowCommand {  get; set; }private string _name;public string Name{get { return _name; }set{_name = value;OnPropertyChanged();}}public MainViewModel(){Name = "Hellow";ShowCommand = new MyCommand(Show);}public void Show(){Name = "点击了按钮";MessageBox.Show(Name);}}
}

3.结果

此时点击按钮之后Name内容也更改。

http://www.lryc.cn/news/399845.html

相关文章:

  • 升级到LVGL9的一些变化(后续发现再补充)
  • 当在多线程环境中使用 C++进行编程时,怎样确保线程安全以及如何处理线程之间的同步和通信?
  • 博物馆地图导航系统:高精度地图引擎与AR/VR融合,实现博物馆数字化转型
  • liunx作业笔记1
  • 大话C语言:第31篇 指针和数组的关系
  • Mysql-索引应用
  • Facebook 开源计算机视觉 (CV) 和 增强现实 (AR) 框架 Ocean
  • 【接口自动化_13课_接口自动化总结】
  • 安防管理平台LntonCVS视频汇聚融合云平台智慧火电厂安全生产管理应用方案
  • 【Web性能优化】在Vue项目中使用defer优化白屏,秒加载!
  • springboot上传图片
  • python入门:python及PyCharm安装
  • 链接追踪系列-04.linux服务器docker安装elk
  • 深入探讨微服务架构设计模式与常见实践
  • 【java】合并数组的两种方法
  • [图解]分析模式-01-概述1
  • 【网络安全】Oracle:SSRF获取元数据
  • Android Bitmap
  • 2024 年全国青少年信息素养大赛 Python 小学组复赛真题
  • C语言——流程控制:if...else、switch...case
  • 小白的OS Copilot 产品测评
  • 使用Scikit-Learn决策树:分类问题解决方案指南
  • E12.【C语言】练习:求两个数的最大公约数
  • Elasticsearch:介绍 retrievers - 搜索一切事物
  • 全面升级的对象创建——抽象工厂模式(Python实现和JAVA实现)
  • 谷粒商城实战笔记-29~34-前端基础 - ES6
  • 浔川官方撤销浔川总社部社长王*职位——浔川官方
  • 小白学python(第七天)
  • npm和yarn清理缓存命令
  • 数据结构之初始二叉树(1)