当前位置:网站首页>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
边栏推荐
- 【畢業季·進擊的技術er】自己的選擇,跪著也要走
- C language operator priority formula
- MySQL的意向共享锁、意向排它锁和死锁
- Summary of qvariant use in QT
- [STL] summary of pair usage
- 【系统】右键桌面图标,转圈后,资源管理器就崩溃,桌面就重新刷新
- /Bin/sh no such file or directory problem
- Network architecture from Wan to sd-wan edge devices
- Some difficulties in making web pages
- Xxl-sso enables SSO single sign on
猜你喜欢

Understand how learning JSX works

QT设计师无法修改窗口大小,无法通过鼠标拖动窗口改变大小的解决方案

VS2013 FFMPEG环境配置及常见错误处理

JS to create an array (all elements are objects)

MySQL Redo log Redo log

使用ts-node直接运行TypeScript代码
![[daily training] 513 Find the value in the lower left corner of the tree](/img/97/ab2179d6dbd0536e8cc139659aecc2.png)
[daily training] 513 Find the value in the lower left corner of the tree

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

XXL-SSO 实现SSO单点登录

杂七杂八的东东
随机推荐
Solve the mining virus sshd2 (redis does not set a password and clear the crontab scheduled task)
Centos7 MySQL records
C语言学习总结
【畢業季·進擊的技術er】自己的選擇,跪著也要走
js中if逻辑过多,常见优化
JS dynamically creates a href circular download file. Only 10 or a fixed number of files can be downloaded
Verilog语法讲解
mingw-w64、msys和ffmpeg的配置与编译
swagger3整合oauth2 认证token
Anti chicken soup speech
DQL、DML、DDL、DCL的概念与区别
idea安装 CloudToolkit 插件
Numerical calculation method chapter7 Calculating eigenvalues and eigenvectors of matrices
C# DPI适配问题
Badly placed()'s problem
关于监督
[daily training] 513 Find the value in the lower left corner of the tree
ssm + ftp +ueditor
Summary of qvariant use in QT
Idea installing the cloudtoolkit plug-in