自己实现一个WPF MVVM 模式 的属性绑定和命令绑定模型

本文介绍了MVVM模式中的属性绑定和命令绑定实现方式。`MvvmNotifyPropertyChanged`抽象类提供了属性改变通知的基础,而`MvvmCommand`实现了依赖于条件的可执行命令。通过这些基础,可以方便地在XAML中进行页面属性绑定和事件响应,简化UI与业务逻辑的交互。
摘要由CSDN通过智能技术生成
    public abstract class MvvmNotifyPropertyChanged : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return;

            field = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

以上是MVVM属性绑定
 

    public class MvvmCommand : ICommand
    {
        public Func<object, bool> CanExecuteCommand = null;
        public Action ExecuteCommand = null;
        public event EventHandler CanExecuteChanged;

        public MvvmCommand(Action executeCommand)
        {
            this.ExecuteCommand = executeCommand;
        }

        public MvvmCommand(Action executeCommand, Func<object, bool> canExecuteCommand)
        {
            this.ExecuteCommand = executeCommand;
            this.CanExecuteCommand = canExecuteCommand;
        }

        public bool CanExecute(object parameter)
        {
            if (CanExecuteCommand != null)
                return CanExecuteCommand(parameter);
            return true;
        }

        public void Execute(object parameter)
        {
            ExecuteCommand?.Invoke();
        }

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty);
        }
    }
    public class MvvmCommand<T> : ICommand
    {
        public Func<T, bool> CanExecuteCommand = null;
        public Action<T> ExecuteCommand = null;
        public event EventHandler CanExecuteChanged;

        public MvvmCommand(Action<T> executeCommand)
        {
            this.ExecuteCommand = executeCommand;
        }

        public MvvmCommand(Action<T> executeCommand, Func<T, bool> canExecuteCommand)
        {
            this.ExecuteCommand = executeCommand;
            this.CanExecuteCommand = canExecuteCommand;
        }

        public bool CanExecute(object parameter)
        {
            if (CanExecuteCommand != null)
                return CanExecuteCommand((T)parameter);
            return true;
        }

        public void Execute(object parameter)
        {
            if (!CanExecute(parameter)) return;
            ExecuteCommand?.Invoke((T)parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            CanExecuteChanged?.Invoke(this, EventArgs.Empty);
        }
    }

以上是命令绑定

使用效果如下:

可以在 xaml 里  引用当前的 viewmodel,方便页面属性引用和运行实例绑定生成。效果不错!

 

 

当然可以,下面是一个基本的WPF MVVM命令绑定实现: 首先,创建一个命令类: ```c# public class RelayCommand : ICommand { private readonly Action<object> _execute; private readonly Predicate<object> _canExecute; public RelayCommand(Action<object> execute, Predicate<object> canExecute = null) { _execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute; } public bool CanExecute(object parameter) { return _canExecute == null || _canExecute(parameter); } public void Execute(object parameter) { _execute(parameter); } public event EventHandler CanExecuteChanged { add => CommandManager.RequerySuggested += value; remove => CommandManager.RequerySuggested -= value; } } ``` 然后在ViewModel中定义需要绑定命令: ```c# public class MainViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private RelayCommand _openCommand; public RelayCommand OpenCommand { get { if (_openCommand == null) { _openCommand = new RelayCommand(p => Open(), p => CanOpen()); } return _openCommand; } } private bool CanOpen() { //检查是否可以执行打开命令 return true; } private void Open() { //执行打开命令 } } ``` 最后,在View中绑定命令: ```xml <Button Content="打开" Command="{Binding OpenCommand}" /> ``` 完成后,当用户单击按钮时,将自动调用ViewModel中的OpenCommand方法,而不需要在View中处理任何逻辑。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝创精英团队

你的支持是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值