///
{
private Predicate < object > canExecute; private Action < object > method; ///:
this (method, null ){
}
///{
this .method = method; this .canExecute = canExecute;}
///{
if (canExecute == null ){
return true ;}
return canExecute(parameter);}
///{
method.Invoke(parameter);
}
///{
var canExecuteChanged = CanExecuteChanged; if (canExecuteChanged != null )canExecuteChanged(
this , e);}
///{
OnCanExecuteChanged(
EventArgs .Empty);}
}
方法一:-----------------------------------------------------------------
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:ViewModel="clr-namespace:Silverlight40.Binding.Command"
Title="Demo Page">
Code部分:-----------------------------------------------------------------------------
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 class MyViewModel
{
// 声明一个 ICommand 类型,用于绑定到 ButtonBase 或 Hyperlink 的 Command 属性上
public ICommand Hello { get; set; }
public MyViewModel()
{
// 绑定了 Hello 的命令被执行时则会调用 ExecuteHello(object parameter) 方法
Hello = new MyCommand(ExecuteHello);
}
private void ExecuteHello(object parameter)
{
MessageBox.Show("Hello: " + parameter.ToString());
}
}
}
或者:-------------------
private DelegateCommand clickNewButton;
public DelegateCommand ClickNewButton
{
get
{
if (clickNewButton == null)
{
clickNewButton = new DelegateCommand((parameter) =>
{
parameter.GetType().GetMethod("Show").Invoke(parameter, null);
});
}
return clickNewButton;
}
set { clickNewButton = value; }
}
方法二:-----------------------------------------------------------------
xmlns :Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"< Interactivity : Interaction.Behaviors > < PopSearchAttributeEntityBehaviors : PopSearchAttributeEntityBehaviors > < Interactivity : Interaction.Triggers > < Interactivity : EventTrigger SourceName ="ButtonCancel" EventName ="Click"> < Interactivity : InvokeCommandAction CommandName ="CancelCommand" /> Interactivity : EventTrigger > < Interactivity : EventTrigger SourceName ="ButtonOK" EventName ="Click"> < Interactivity : InvokeCommandAction CommandName ="OkClickedCommand" /> Interactivity : EventTrigger > Interactivity : Interaction.Triggers > PopSearchAttributeEntityBehaviors : PopSearchAttributeEntityBehaviors > Interactivity : Interaction.Behaviors >
< Interactivity : Interaction.Triggers > < Interactivity : EventTrigger SourceName ="DataGridSearchResult" EventName ="CheckBoxSelected"> < Interactivity : InvokeCommandAction Command ="{ Binding AttributesSelectedCommand }" /> Interactivity : EventTrigger > Interactivity : Interaction.Triggers >
Behavior的Code部分:-----------------------------------------------
public class PopSearchAttributeEntityBehaviors : Behavior < ChildWindow >{
private Collection < object > dataGridSelectRows; public Collection < object > DataGridSelectRows{
get { return dataGridSelectRows; } set { dataGridSelectRows = value ; }}
///:
base (){
CancelCommand =
new DelegateCommand (OnCancel);OkClickedCommand =
new DelegateCommand (OnOkClicked);}
///{
(
this .AssociatedObject as PopSearchAttributeEntity ).Close();}
///{
ObservableCollection < object > selectedItems = (( this .AssociatedObject as PopSearchAttributeEntity ).DataContext as PopSearchAttributeEntityViewModel ).SelectedAttributes;((
this .AssociatedObject as PopSearchAttributeEntity ).DataContext as PopSearchAttributeEntityViewModel ).OkCallback(selectedItems);(
this .AssociatedObject as PopSearchAttributeEntity ).Close();}
}