介绍
Silverlight 4.0 MVVM 模式:
* ICommand - 命令。可以将其绑定到 ButtonBase 或 Hyperlink 的 Command 属性上
* MVVM 模式 - Model-View-ViewModel
在线DEMO
http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
示例
1、演示 ICommand 的应用
MyCommand.cs
代码
/*
* ICommand - 命令。可以将其绑定到ButtonBase或Hyperlink的Command属性上
* bool CanExecute(object parameter) - 当请命令是否可以执行
* event EventHandler CanExecuteChanged - 当请命令是否可以执行的状态发生改变时所触发的事件
* void Execute(object parameter) - 当前命令被执行时,所调用的方法
*/
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Silverlight40.Binding.Command
{
public delegate void Execute(object parameter);
public delegate bool CanExecute(object parameter);
public class MyCommand : ICommand
{
private Execute _executeMethod;
private CanExecute _canExecuteMethod;
public MyCommand()
: this(null, null)
{
}
public MyCommand(Execute executeMethod)
: this(executeMethod, null)
{
}
public MyCommand(Execute executeMethod, CanExecute canExecuteMethod)
{
_executeMethod = executeMethod;
_canExecuteMethod = canExecuteMethod;
}
public bool CanExecute(object parameter)
{
if (_canExecuteMethod == null)
return true;
else
return _canExecuteMethod(parameter);
}
public void Execute(object parameter)
{
if (_executeMethod != null)
_executeMethod(parameter);
}
public event EventHandler CanExecuteChanged;
protected virtual void OnCanExecuteChanged(EventArgs e)
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, e);
}
public void RaiseCanExecuteChanged()
{
OnCanExecuteChanged(EventArgs.Empty);
}
}
}