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&lt;T&gt;"/> 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&lt;T&gt;"/> 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&lt;T&gt;"/> 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&lt;T&gt;"/> 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
    }
}

 

 以上为自己试着写的例子,上面绿色加粗部分,我还是没明白,希望大神解释。

posted @ 2012-07-16 21:41  爱听相声的民工  阅读(1203)  评论(0编辑  收藏  举报