zoukankan      html  css  js  c++  java
  • wpf的mvvm

    INotifyPropertyChanged,用来绑定字段

    /// <summary>
        /// mvvm的基类
        /// </summary>
        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,用来绑定事件

        /// <summary>
        /// 执行命令
        /// </summary>
        public class DelegateCommand : ICommand
        {
            public event EventHandler CanExecuteChanged;
            public Action<object> ExecuteAction;
            public Func<object, bool> CanExecuteFunc;
    
            public DelegateCommand()
            {
    
            }
    
            public DelegateCommand(Action<object> execute) : this(execute, null)
            {
            }
    
            public DelegateCommand(Action<object> execute, Func<object, 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;
            /// <summary>
            /// 端口名称
            /// </summary>
            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

  • 相关阅读:
    java基础之System类
    java基础之System类
    java基础之Random类
    java基础之Math类
    java基础之Math类
    java基础之Character类概述
    MySQL数据库
    JavaScript面向对象与原型
    PHP
    sass基础用法
  • 原文地址:https://www.cnblogs.com/shuaimeng/p/13594745.html
Copyright © 2011-2022 走看看