MVVMlight框架应用:Data Binding、Command

常用Wpf开发中我们在ViewModel中实现INotifyPropertyChanged接口,通过触发PropertyChanged事件达到通知UI更改的目的;在MVVMLight框架里,这里我们定义的ViewModel都继承自ViewModelBaseViewModelBase封装在MvvmLight框架中,它已经实现了INotifyPropertyChanged接口,因此我们在定义ViewModel属性时,只需要调用RaisePropertyChanged(PropertyName)就可以进行属性更改通知了。例如下代码:

       // DatePicker 选中日期
        private DateTime _SelectedDate;
        public DateTime SelectedDate
        {
            get
            {
                return _SelectedDate;
            }

            set
            {
                if (_SelectedDate == value)
                    return;

                _SelectedDate = value;

                RaisePropertyChanged("SelectedDate");
            }
        }

 事件是WPF/SL应用程序中UI与后台代码进行交互的最主要方式,与传统方式不同,mvvm中主要通过绑定到命令来进行事件的处理,因此要了解mvvm中处理事件的方式,就必须先熟悉命令的工作原理。

一、RelayCommand命令

WPF/SL命令是通过实现 ICommand 接口创建的。 ICommand 公开两个方法(ExecuteCanExecute)和一个事件(CanExecuteChanged)。 Execute 执行与命令关联的操作。CanExecute 确定是否可以在当前命令目标上执行命令。在MvvmLight中实现ICommand接口的类是RelayCommand,RelayCommand通过构造函数初始化ExecuteCanExecute方法,因此,构造函数传入的是委托类型的参数,ExecuteCanExecute则执行的是委托的方法,RelayCommand相关代码如下:

        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }

            _execute = execute;
            _canExecute = canExecute;
        }

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

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

二、 Comand属性绑定

简单的例子:一个TextBox和一个Button,TextBox非空内容时候Button才可用,可用的Button点击后把TextBox内容show出来。

<Window x:Class="MVVMLightDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MVVMLightDemo.ViewModel"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowViewModel></local:MainWindowViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBox Text="{Binding UserName, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <Button Content="show" Command="{Binding ShowCommand}" CommandParameter="{Binding UserName}"></Button>
        </StackPanel>
    </Grid>
</Window>

ViewMode:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Windows.Input;
using System.Windows;
namespace MVVMLightDemo.ViewModel
{
    public class MainWindowViewModel:ViewModelBase
    {
        private string _UserName;

        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; RaisePropertyChanged("UserName"); }
        }
        public ICommand ShowCommand
        {
            get
            {
                return new RelayCommand<string>(
                    (user) => {
                        MessageBox.Show(user);
                    }, (user) =>
                    {
                        return !string.IsNullOrEmpty(user);
                    });

            }
        }
    }
}

 

posted @ 2013-08-13 15:02  xiepeixing  阅读(3202)  评论(0编辑  收藏  举报