当前位置:网站首页>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. 指令执行后,属性同步改变,使前端显示同步改变
边栏推荐
- Open source ecology 𞓜 super practical open source license basic knowledge literacy post (Part 2)
- C language removes line breaks (or other characters) at the end of strings
- Day_ 02 smart communication health project - appointment management - inspection item management
- 剑指 Offer 42. 连续子数组的最大和
- Haas506 2.0 development tutorial -sntp (only versions above 2.2 are supported)
- 设计师需要懂的数据指标与数据分析模型
- Sword finger offer 42 Maximum sum of successive subarrays
- Get to know webassembly quickly
- haas506 2.0开发教程-hota(仅支持2.2以上版本)
- 20220620 uniformly completely observable (UCO)
猜你喜欢

从 WAN 到 SD-WAN 边缘设备的网络架构

杂七杂八的东东

JS to create an array (all elements are objects)

Day_ 07 smart communication health project FreeMarker

Haas506 2.0 development tutorial -hota (only supports versions above 2.2)

Sword finger offer 42 Maximum sum of successive subarrays

bootstrap如何清除浮动的样式

Docker practice - redis cluster deployment and micro service deployment project

【接口自动化】软件测试涨薪核心技能、让薪资涨幅200%

Softing dataFEED OPC Suite将西门子PLC数据存储到Oracle数据库中
随机推荐
20220620 uniformly completely observable (UCO)
Steam教育对国内大学生的影响力
百度URL参数之LINK?URL参数加密解密研究(代码实例)
Test of ers function under the supplier consignment purchase mode of SAP mm
Qt 中 QVariant 使用总结
图解 Google V8 # 17:消息队列:V8是怎么实现回调函数的?
Haas506 2.0 development tutorial - Advanced Component Library -modem Voicecall (only supports versions above 2.2)
Day_ 08 smart health project - mobile terminal development - physical examination appointment
2022-01-12: give a positive array arr, length N, subscript 0~n-1, a
2.17 haas506 2.0 development tutorial system (only versions above 2.2 are supported)
mingw-w64、msys和ffmpeg的配置与编译
光谱共焦的测量原理及厚度测量模式
C language removes line breaks (or other characters) at the end of strings
golang正则regexp包使用-04-使用正则替换(ReplaceAll(),ReplaceAllLiteral(),ReplaceAllFunc())
Verilog语法讲解
Plot+seaborn+folium: a visual exploration of Abbey's rental housing data
SAP execution transaction code mrrl error -no message was found for partner 100065-
Vs+qt project transferred to QT Creator
二叉树的遍历及相关知识
phpStudy设置301重定向