当前位置:网站首页>WPF Command指令和INotifyPropertyChanged
WPF Command指令和INotifyPropertyChanged
2022-06-23 05:34:00 【大大打打】
MainViewMode.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WPFTest1
{
// INotifyPropertyChanged 让Name和Title变化之后,前台能够显示变化
public class MainViewMode : INotifyPropertyChanged
{
public MainViewMode()
{
// 构造函数初始化会动态改变的值
Name = "Hello";
Title = "标题";
// 使用
ShowCommand = new MyCommand(show);
}
// 定义之后后台才能Binding
public MyCommand ShowCommand { get; set; }
// 属性Name
private string name;
public string Name
{ get { return name; }
set { name = value;
// 属性产生变化后使前台显示同步改变
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
// 属性Title
private string title;
public string Title
{
get { return title; }
set
{
title = value;
// 属性产生变化后使前台显示同步改变
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Title"));
}
}
// 进行值得改变
public void show()
{
Name = "点击了按钮";
Title = new Random().Next(1, 10).ToString();
MessageBox.Show(Name);
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
MainWindow.xaml
<Grid>
<StackPanel>
<TextBox Text="{Binding Name}"/>
<TextBox Text="{Binding Title}"/>
<Button Content="Show" Command="{Binding ShowCommand}"/>
</StackPanel>
</Grid>MainWindow.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;
namespace WPFTest1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 定义上下文数据,使MainViewMode成为其数据来源
this.DataContext = new MainViewMode();
}
}
}
MyCommand.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WPFTest1
{
// 自定义一个Command,必须继承ICommand
public class MyCommand : ICommand
{
// 一个委托
Action executeAction;
// MainViewMode.cs中 MainViewMode构造方法传入的委托 由 MyCommand 构造方法进行接收
public MyCommand(Action action)
{
// 接收委托
executeAction = action;
}
// 实现接口 ICommand 时自带的
public event EventHandler CanExecuteChanged;
// 实现接口 ICommand 时自带的,return true;是自己写的,true表示可执行
// 程序执行后,点击按钮, 工具此处返回的值判断是否能够执行
public bool CanExecute(object parameter)
{
return true;
}
// 程序执行后, 如果能够执行则开始执行,执行的是 MyCommand构造函数那边 传入的一个委托
public void Execute(object parameter)
{
// 执行委托:show (从 MainViewMode.cs传入)
executeAction();
}
}
}
点击按钮之前:
点击按钮之后 :Show上面的文本框发生了变化并出现弹窗

流程梳理:
1. 创建页面,后端 ViewMode 设置属性,前端 xaml 绑定属性
2. 自定义 MyCommand 指令,使按钮绑定该指令,按下时触发该指令
3. 按下按钮,执行绑定的指令,指令调用方法改变值,以委托的形式传递,最后执行传递过来方法,看似是执行指令Command, 实际是执行以委托形式传递到 MyCommand 的中的方法
4. 指令执行后,属性同步改变,使前端显示同步改变
边栏推荐
- Linked Storage
- 如何查看本机IP
- Fastdata pole | insight report on e-commerce consumption of young Chinese users 2021
- mingw-w64、msys和ffmpeg的配置与编译
- For non dpdk kvm1.0 machines, set init Maxrxbuffers changed from 256 to 1024 to improve packet receiving capacity
- Explain csma/cd, token bus and token ring clearly
- Mysql5.6 (5.7-8) is based on shardingsphere5.1.1 sharding proxy mode. Read / write separation
- Laravel log channel grouping configuration
- Easy EDA #学习笔记09# | ESP32-WROOM-32E模组ESP32-DevKitC-V4开发板 一键下载电路
- 图解 Google V8 # 18 :异步编程(一):V8是如何实现微任务的?
猜你喜欢
百度URL參數之LINK?URL參數加密解密研究(代碼實例)

Day_ 05 smart communication health project - appointment management - appointment settings

了解学习 JSX 的工作方式

Day_ 07 smart communication health project FreeMarker

什么是客户体验自动化?

云盒子联合深信服,为南京一中打造智慧双模教学资源分享平台

项目_过滤器Filter解决中文乱码

Laravel log channel 分组配置

mingw-w64、msys和ffmpeg的配置与编译

聚焦智慧城市,华为携手中科星图共同开拓数字化新蓝海
随机推荐
Haas506 2.0 development tutorial - Advanced Component Library -modem Net (only supports versions above 2.2)
记一次GLIB2.14升级GLIB2.18的记录以及其中的步骤原理
mars3d点线面的绘制和重置
Network architecture from Wan to sd-wan edge devices
C # database reports errors. Let's have a look
Open source to the world (Part 2): the power of open source from the evolution of database technology BDTC 2021
C# DPI适配问题
Link of Baidu URL Parameters? Recherche sur le chiffrement et le décryptage des paramètres d'URL (exemple de Code)
二叉树的遍历及相关知识
asp.net文件下载demo与相关问题的处理
Focusing on the smart city, Huawei cooperates with China Science and technology Xingtu to jointly develop a new digital blue ocean
综合培养学生脑力思维的少儿编程
剑指 Offer 42. 连续子数组的最大和
如何实现与FDA保持邮件通信安全加密?
Phpstudy set 301 redirection
C# wpf 附加属性实现界面上定义装饰器
The central network and Information Technology Commission issued the National Informatization Plan for the 14th five year plan, and the network security market entered a period of rapid growth
Haas506 2.0 development tutorial -hota (only supports versions above 2.2)
JSON to proto
业务逻辑安全思路总结