六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 346|回复: 0

MVVM_WPF

[复制链接]

升级  9.33%

16

主题

16

主题

16

主题

秀才

Rank: 2

积分
64
 楼主| 发表于 2013-1-3 15:36:32 | 显示全部楼层 |阅读模式
<div id="cnblogs_post_body">新建立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
    }
}

以上为自己试着写的例子,上面绿色加粗部分,我还是没明白,希望大神解释。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表