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!");
        }
    }
}
相关推荐
luj_17682 小时前
毒素驱动的生物自毁机制探析
服务器·c语言·开发语言·经验分享·算法
万年咸鱼2 小时前
Java List 接口详解:从基础到实战
java·windows·list
海兰2 小时前
【 Python 量化交易】第6章:数据可视化
开发语言·python·信息可视化
多多鼠2 小时前
Tool Calling的信任边界:从协议校验到执行沙箱的完整链路设计
运维·开发语言·网络·人工智能·python·langchain
Java的搬运工2 小时前
Spring boot 事件监听
java·spring boot·spring·编程语言
啊阿狸不会拉杆2 小时前
《计算机网络-自顶向下方法》1.5 协议层次及其服务模型 读书笔记
开发语言·计算机网络·php
tryxr2 小时前
Chat2Excel 项目网关服务开发
java·gateway·网关模块开发
谢亮_vipxieliang2 小时前
ValidX vs Apache Commons Validator:功能与性能对比
java·服务器·spring boot·后端·spring cloud·apache·hibernate
tryxr2 小时前
Chat2Excel 项目通用服务开发
java·开发语言·excel·java项目开发
小裕哥略帅2 小时前
Spring AOP 实现通用 Token 自动刷新重试工具包
java·后端·spring