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!");
        }
    }
}
相关推荐
小白-Tester几秒前
2026最新Postman安装教程[简单易懂]附安装包
开发语言·lua
xiaoye37081 分钟前
某大厂java面试题一面20260313
java
啦啦啦_999913 分钟前
13. AI面试题之 Dify
java
春日见15 分钟前
端到端大模型自动驾驶
java·开发语言·驱动开发·docker·自动驾驶·计算机外设
rell33622 分钟前
机顶盒播放udp/rtp马赛克
java·网络·网络协议·udp
Arya_aa23 分钟前
多个对象通过集合实现io流的读写
java
A.A呐25 分钟前
【QT第三章】常用控件2
开发语言·qt
白云如幻33 分钟前
【JDBC】集合、反射和泛型复习-3
java·开发语言·jdbc
笨笨马甲36 分钟前
Qt 实现三维坐标系的方法
开发语言·qt
bugcome_com1 小时前
C# 高级集合使用示例
开发语言·c#