当前位置:网站首页>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
边栏推荐
猜你喜欢

Network architecture from Wan to sd-wan edge devices

【STL】pair用法总结

Summary of qvariant use in QT

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

Intentional shared lock, intentional exclusive lock and deadlock of MySQL

【STL】关联容器之map用法总结

Eureka

xml schem 记录

【BULL中文文档】用于在 NodeJS 中处理分布式作业和消息的队列包

Storage mode of data in memory (C language)
随机推荐
如何迁移virtualbox 的虚拟机到hype-v
centos7 mysql 记录
Drawing and resetting of mars3d point, line and surface
【项目实训】线形组件的细节
How to view native IP
994. 腐烂的橘子-非递归法
2121. sum of intervals of the same elements - hash table method
Cetos7 record
Summary of qvariant use in QT
[STL] summary of pair usage
idea安装 CloudToolkit 插件
Focusing on the smart city, Huawei cooperates with China Science and technology Xingtu to jointly develop a new digital blue ocean
[QT] basic learning notes
【BULL中文文档】用于在 NodeJS 中处理分布式作业和消息的队列包
xml dtd 记录
MySQL重做日志 redo log
数据在内存中的存储方式(C语言)
网页制作存在的一些难点
C language operator priority formula
Anti chicken soup speech