当前位置:网站首页>WPF command directive and inotifypropertychanged
WPF command directive and inotifypropertychanged
2022-06-23 06:59:00 【Big fight】
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 Give Way Name and Title After the change , The front desk can display changes
public class MainViewMode : INotifyPropertyChanged
{
public MainViewMode()
{
// Constructor initializes values that change dynamically
Name = "Hello";
Title = " title ";
// Use
ShowCommand = new MyCommand(show);
}
// After definition, the background can Binding
public MyCommand ShowCommand { get; set; }
// attribute Name
private string name;
public string Name
{ get { return name; }
set { name = value;
// After the property changes, the foreground display changes synchronously
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
// attribute Title
private string title;
public string Title
{
get { return title; }
set
{
title = value;
// After the property changes, the foreground display changes synchronously
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Title"));
}
}
// Make a worthwhile change
public void show()
{
Name = " Click the button ";
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 Interaction logic of
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Define context data , send MainViewMode Become its data source
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
{
// Customize a Command, Must inherit ICommand
public class MyCommand : ICommand
{
// A commission
Action executeAction;
// MainViewMode.cs in MainViewMode Delegate passed in by constructor from MyCommand Construct a method to receive
public MyCommand(Action action)
{
// Receiving entrustment
executeAction = action;
}
// Implementation interface ICommand It comes with you
public event EventHandler CanExecuteChanged;
// Implementation interface ICommand It comes with you ,return true; I wrote it myself ,true For executables
// After the program is executed , Click button , The value returned by the tool here determines whether it can execute
public bool CanExecute(object parameter)
{
return true;
}
// After the program is executed , If it can be executed, start to execute , Execution is MyCommand Constructor side An incoming delegate
public void Execute(object parameter)
{
// Perform entrusted :show ( from MainViewMode.cs Pass in )
executeAction();
}
}
}
Before clicking the button :
After clicking the button :Show The text box above changes and a pop-up window appears

Process carding :
1. Create a page , Back end ViewMode Set properties , front end xaml Binding properties
2. Customize MyCommand Instructions , Bind the button to the instruction , This command is triggered when pressed
3. Press the button , Perform binding Instructions , Instructions Calling method Change the value , With entrust In the form of Pass on , Last perform Pass it on Method , It seems to be executing instructions Command, the truth is that Execution is passed as a delegate to MyCommand Method in
4. After the command is executed , Properties change synchronously , Make the front display change synchronously
边栏推荐
- system 权限程序不能访问sd卡问题
- idea自动生成serialVersionUID
- MySQL optimization
- Easy EDA learning notes 09 esp32-wroom-32e module esp32-devkitc-v4 development board one click download circuit
- 746. 使用最小花费爬楼梯-动态规划
- Termux
- mingw-w64、msys和ffmpeg的配置与编译
- 解决挖矿病毒 sshd2(redis未设密码、清除crontab定时任务)
- JS to create an array (all elements are objects)
- Swagger3 integrates oauth2 authentication token
猜你喜欢

开源OAuth2框架 实现SSO单点登录

leetcode - 572. 另一棵树的子树

MySQL optimization

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

Badly placed ()‘s 问题

xml schem 记录

Focusing on the smart city, Huawei cooperates with China Science and technology Xingtu to jointly develop a new digital blue ocean

ssm + ftp +ueditor

Some difficulties in making web pages

Usage Summary of item views and item widgets controls in QT
随机推荐
Measurement principle and thickness measurement mode of spectral confocal
[STL] summary of stack and queue usage of container adapter
XShell7 下载
Numerical calculation method chapter7 Calculating eigenvalues and eigenvectors of matrices
Children's programming for comprehensively cultivating students' mental thinking
【BULL中文文档】用于在 NodeJS 中处理分布式作业和消息的队列包
asp.net文件下载demo与相关问题的处理
Linux安装mysql8.0.25
[bull Chinese document] queue package used to process distributed jobs and messages in nodejs
利用adb 调试设备串口信息的一个小方法
Chrome删除重复书签
MySQL重做日志 redo log
【畢業季·進擊的技術er】自己的選擇,跪著也要走
Detailed explanation of callback function
[project training] change of linear arrow
Badly placed ()‘s 问题
MySQL optimization
MySQL redo log redo log
QT中的item views与Item widgets控件的用法总结
leetcode - 572. A subtree of another tree