打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
MVVM_WPF

新建立WPF工程:工程名字为Study

1、MainWindow.xaml:

<Window x:Class="Study.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Study"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:StudentViewControl></local:StudentViewControl>
    </Window.DataContext>
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="38,103,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding UpdateStudentName}"  />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="38,51,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding StudentName}" />
    </Grid>
</Window>

2、Student.cs

public class Student
    {
        public string studentName { get; set; }
    }

3、

public class StudentViewControl : INotifyPropertyChanged
    {
        private Student _student;
        public Student Student
        {
            get { return _student; }
            set { this._student = value; }
        }

        public StudentViewControl()
        {
            this._student = new Student();
            this.Student.studentName = "jaygj";
        }


        public string StudentName
        {
            get
            {
                return _student.studentName;
            }
            set
            {
                this._student.studentName = value;
                OnPropertyChanged();
            }
        }

        public void OnPropertyChanged()
        {
            PropertyChangedEventHandler hander = PropertyChanged;
            if (hander != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("StudentName"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public ICommand UpdateStudentName { get { return new RelayCommand(UpdateStudentNameExecute, null); } }为null在下面的公公类解析中为ture,不多解释。

        或者为

       public ICommand UpdateStudentName { get { return new RelayCommand(UpdatStudentNameExecute, GetStatus); } }

        public void UpdateStudentNameExecute()
        {
            this.StudentName = "hello world";          
        }

        public bool GetStatus()
        {
            return true;
        }
    }

 

4.公公类:

//  Original author - Josh Smith - http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030

using System;
using System.Diagnostics;
using System.Windows.Input;

namespace MicroMvvm
{
    /// <summary>
    /// A command whose sole purpose is to relay its functionality to other objects by invoking delegates. The default return value for the CanExecute method is 'true'.
    /// </summary>
    public class RelayCommand<T> : ICommand
    {

        #region Declarations

        readonly Predicate<T> _canExecute;
        readonly Action<T> _execute;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand<T>"/> class and the command can always be executed.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action<T> execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand<T>"/> class.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action<T> execute, Predicate<T> canExecute)
        {

            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }

        #endregion

        #region ICommand Members

        public event EventHandler CanExecuteChanged
        {
            add
            {

                if (_canExecute != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {

                if (_canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        [DebuggerStepThrough]
        public Boolean CanExecute(Object parameter)
        {
            return _canExecute == null ? true : _canExecute((T)parameter);
        }

        public void Execute(Object parameter)
        {
            _execute((T)parameter);
        }

        #endregion
    }

    /// <summary>
    /// A command whose sole purpose is to relay its functionality to other objects by invoking delegates. The default return value for the CanExecute method is 'true'.
    /// </summary>
    public class RelayCommand : ICommand
    {

        #region Declarations

        readonly Func<Boolean> _canExecute;
        readonly Action _execute;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand<T>"/> class and the command can always be executed.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand<T>"/> class.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action execute, Func<Boolean> canExecute)
        {

            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }

        #endregion

        #region ICommand Members

        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {

                if (_canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        [DebuggerStepThrough]
        public Boolean CanExecute(Object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        public void Execute(Object parameter)
        {
            _execute();
        }

        #endregion
    }
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
MVVM中的命令绑定及命令参数
WPF customize DelegateCommand
走进WPF之MVVM完整案例
可以减少此代码中使用的构造函数的数量吗?构造函数重载和多个类的转发
MyBatis(1):MyBatis入门
WPF MVVM实例一
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服