当前位置: 首页 > news >正文

WPF自定义命令及属性改变处理

1、项目建构
在这里插入图片描述

2、自定义命令

namespace WpfDemo.Base
{public class MyCommand : ICommand{Action executeAction;public MyCommand(Action action){executeAction = action;}public event EventHandler? CanExecuteChanged;public bool CanExecute(object? parameter){return true;}public void Execute(object? parameter){executeAction();}}
}

3、属性改变

namespace WpfDemo.Base
{public class ViewModelBase : INotifyPropertyChanged{public event PropertyChangedEventHandler? PropertyChanged;public void OnPropertyChanged([CallerMemberName] string propertyName = ""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}
}

4、界面

<Window x:Class="WpfDemo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfDemo"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><StackPanel><TextBox Height="30" Margin="5" Text="{Binding InputContent}"/><TextBox Height="30" Margin="5" Text="{Binding OutputContent}"/><Button Height="30" Width="75" Content="Button" Command="{Binding ShowCommand}"/></StackPanel>
</Window>

5、后台代码

namespace WpfDemo
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new MainViewModel();}}
}

6、ViewModel代码

namespace WpfDemo.ViewModels
{public class MainViewModel:ViewModelBase{public MainViewModel(){ShowCommand = new MyCommand(Show);}private string? inputContent;public string? InputContent{get { return inputContent; }set { inputContent = value; OnPropertyChanged(); }}private string? outputContent;public string? OutputContent{get { return outputContent; }set { outputContent = value; OnPropertyChanged(); }}public MyCommand ShowCommand { get; set; }public void Show(){OutputContent = InputContent;MessageBox.Show("It is blue sky!");}}
}
http://www.lryc.cn/news/139793.html

相关文章:

  • macbook m1 docker中使用go
  • 【Hello Network】DNS协议 NAT技术 代理服务器
  • Android 使用模拟器模拟Linux操作系统
  • 机器学习基础之《分类算法(5)—朴素贝叶斯算法原理》
  • # Go学习-Day6
  • 分布式 - 服务器Nginx:一小时入门系列之 HTTPS协议配置
  • 探秘Linux系统性能监控神器!Linux和Python技术持续学习者必看!
  • 文心一言续写太监小说《名侦探世界的巫师》
  • Solidity 合约安全,常见漏洞(第三篇)
  • Linux安装Redis数据库,无需公网IP实现远程连接
  • 智慧政务,长远布局——AIGC引领,加速推进数字化政府建设
  • 中央处理器(CPU):组成、指令周期、数据通路、控制方式、控制器、指令流水线,补充(多处理器系统、硬件多线程)
  • 开源微服务如何选型?Spring Cloud、Dubbo、gRPC、Istio 详细对比
  • Nginx的HTTPS部署与安全性能优化
  • 5.8. Trusted Board Boot
  • 微信小程序——van-field中的left-icon属性自定义
  • 一文学会lua脚本
  • 学习JAVA打卡第四十二天
  • 2023-8-25食物链
  • 为什么要使用IP地址进行定位
  • CSS概念
  • 淘宝API技术解析,实现关键词搜索淘宝商品(商品详情接口等)
  • Redis 7 教程 数据类型 基础篇
  • -bash: tree: command not found 的解决方法
  • SPI总线协议
  • Ubuntu20.04配置mysql配置主从复制
  • HTTP 服务器(基于go实现)
  • 【整数二分】
  • 开发一款AR导览导航小程序多少钱?ar地图微信小程序 ar导航 源码
  • Shell 编程快速入门 之 函数基础知识