///
<summary> /// DelegateCommand /// </summary> public class DelegateCommand : ICommand{
private Predicate<object> canExecute; private Action<object> method; /// <summary> /// Occurs when changes occur that affect whether the command should execute. /// </summary> public event EventHandler CanExecuteChanged; /// <summary> /// Initializes a new instance of the <see cref="DelegateCommand"/> class. /// </summary> /// <param name="method">The method.</param> public DelegateCommand(Action<object> method):
this(method, null){
}
/// <summary> /// Initializes a new instance of the <see cref="DelegateCommand"/> class. /// </summary> /// <param name="method">The method.</param> /// <param name="canExecute">The can execute.</param> public DelegateCommand(Action<object> method, Predicate<object> canExecute){
this.method = method; this.canExecute = canExecute;}
/// <summary> /// Defines the method that determines whether the command can execute in its current state. /// </summary> /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param> /// <returns> /// true if this command can be executed; otherwise, false. /// </returns> public bool CanExecute(object parameter){
if (canExecute == null){
return true;}
return canExecute(parameter);}
/// <summary> /// Defines the method to be called when the command is invoked. /// </summary> /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param> public void Execute(object parameter){
method.Invoke(parameter);
}
/// <summary> /// Raises the <see cref="E:CanExecuteChanged"/> event. /// </summary> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> protected virtual void OnCanExecuteChanged(EventArgs e){
var canExecuteChanged = CanExecuteChanged; if (canExecuteChanged != null)canExecuteChanged(
this, e);}
/// <summary> /// Raises the can execute changed. /// </summary> public void RaiseCanExecuteChanged(){
OnCanExecuteChanged(
EventArgs.Empty);}
}
方法一:-----------------------------------------------------------------
<navigation:Page x:Class="Silverlight40.Binding.Command.Demo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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">
<Grid x:Name="LayoutRoot">
<Grid.DataContext>
<ViewModel:MyViewModel />
</Grid.DataContext>
<!--
Command - 指定需要关联的命令
CommandParameter - 传递给 Command 的参数
-->
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal" Height="20">
<TextBox Name="txtName" Text="webabcd" />
<Button Content="Hello" Command="{Binding Hello}" CommandParameter="{Binding ElementName=txtName, Path=Text }" />
</StackPanel>
</Grid>
</navigation: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; }}
/// <summary> /// Close Menu Command /// </summary> public ICommand CancelCommand { get; set; } public ICommand OkClickedCommand { get; set; } public PopSearchAttributeEntityBehaviors():
base(){
CancelCommand =
new DelegateCommand(OnCancel);OkClickedCommand =
new DelegateCommand(OnOkClicked);}
/// <summary> /// button Cancel /// </summary> /// <param name="state">StackPanel Menu Object</param> public void OnCancel(object menu){
(
this.AssociatedObject as PopSearchAttributeEntity).Close();}
/// <summary> /// button ok click /// </summary> /// <param name="menu"></param> public void OnOkClicked(object menu){
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();}
}