WPF/MVVM学习笔记(2)

2 绑定command

第一步,在viewmodel声明一个ICommand对象

第二步,创建一个command调用的方法

第三步,实例化声明的ICommand对象

          private string showtext;

        public string ShowText
        {
            get { return showtext; }
            set { Set(ref showtext , value); }
        }
        public ICommand testCommand { get; set; }
        /// 
        /// Initializes a new instance of the MvvmViewModel class.
        /// 
        public MvvmViewModel()
        {
            ShowText = "good job!";
            testCommand = new RelayCommand(testCommandMethod);
        }

        private void testCommandMethod()
        {
            ShowText = "command work!!!!!";
        }

第四步, 在View上创建一个button,并将声明的icommand对象绑定到button上

        

WPF/MVVM学习笔记(2)_第1张图片

你可能感兴趣的:(c#,wpf)