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

1、项目建构

2、自定义命令

csharp 复制代码
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、属性改变

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

4、界面

csharp 复制代码
<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、后台代码

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

6、ViewModel代码

csharp 复制代码
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!");
        }
    }
}
相关推荐
一只码代码的章鱼4 分钟前
Spring 的 异常管理的相关注解@ControllerAdvice 和@ExceptionHandler
java·后端·spring
qqxhb9 分钟前
零基础学Java——第十一章:实战项目 - 微服务入门
java·开发语言·spring cloud·微服务
androidwork15 分钟前
Arrow库:函数式编程在Kotlin Android中的深度实践
android·java·kotlin
离别又见离别19 分钟前
java实现根据Velocity批量生成pdf并合成zip压缩包
java·pdf
码农飞哥24 分钟前
互联网大厂Java求职面试实战:Spring Boot到微服务的技术问答解析
java·spring boot·缓存·面试·消息队列·技术栈·microservices
CodeCraft Studio26 分钟前
国产化Word处理控件Spire.Doc教程:通过C# 删除 Word 文档中的超链接
开发语言·c#·word
martian66539 分钟前
麒麟系统下Tomcat部署Java Web程序(WAR包)及全链路问题排查指南
开发语言·tomcat·系统安全
ai.Neo40 分钟前
牛客网NC22012:判断闰年问题详解
开发语言·c++·算法
曼岛_1 小时前
[Java实战]Spring Boot + Netty 实现 TCP 长连接客户端及 RESTful 请求转发(二十六)
java·spring boot·tcp/ip
好吃的肘子1 小时前
ElasticSearch进阶
大数据·开发语言·分布式·算法·elasticsearch·kafka·jenkins