wpf的mvvm

2020-12-20 02:21

阅读:669

标签:ane   serial   prope   调用   class   执行命令   hang   fun   invoke   

INotifyPropertyChanged,用来绑定字段

/// 
    /// mvvm的基类
    /// 
    public class NotificationOjbect : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string PropertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    }

ICommand,用来绑定事件

    /// 
    /// 执行命令
    /// 
    public class DelegateCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;
        public Actionobject> ExecuteAction;
        public Funcobject, bool> CanExecuteFunc;

        public DelegateCommand()
        {

        }

        public DelegateCommand(Actionobject> execute) : this(execute, null)
        {
        }

        public DelegateCommand(Actionobject> execute, Funcobject, bool> canExecute)
        {
            if (execute == null)
            {
                return;
            }
            ExecuteAction = execute;
            CanExecuteFunc = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (this.CanExecuteFunc == null)
            {
                return true;
            }
            return this.CanExecuteFunc(parameter);
        }

        public void Execute(object parameter)
        {
            if (this.ExecuteAction != null)
            {
                this.ExecuteAction(parameter);
            }
        }
    }

属性的绑定

 private string _PortName;
        /// 
        /// 端口名称
        /// 
        public string PortName
        {
            get { return _PortName; }
            set
            {
                _PortName = value;
                _notification.RaisePropertyChanged("PortName");
            }
        }

事件的绑定

public ICommand SaveSerialPort
        {
            get
            {
                return new DelegateCommand(
                    (param) =>
                    {
                        btnSave_Click(param);
                    }
                   , (v) => { return true; });
            }
        }

调用代码

this.DataContext = new SerialPortViewModel();

界面绑定属性和事件

技术图片

 

一个最简单的wpf的mvvm就弄完了

技术图片

dome的地址:https://gitee.com/cainiaoA/wpfstudent

 

wpf的mvvm

标签:ane   serial   prope   调用   class   执行命令   hang   fun   invoke   

原文地址:https://www.cnblogs.com/shuaimeng/p/13594745.html


评论


亲,登录后才可以留言!