一、Delegatecommand收录
///<summary>
/// DelegateCommand
/// </summary>
public class DelegateCommand : ICommand
{
private Predicate<object> canExecute;
private Action<object> executeAction;
/// <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="executeAction">The method.</param>
public DelegateCommand(Action<object> executeAction) : this(executeAction, 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> executeAction, Predicate<object> canExecute)
{
this.executeAction = executeAction;
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)
{
executeAction.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);
}
}
using System;
using System.Windows.Input;
namespace ScoreTools.ViewModels
{
/// <summary>
/// Delegatecommand,这种WPF.SL都可以用,VIEW里面直接使用INTERACTION的trigger激发。比较靠谱,适合不同的UIElement控件
/// </summary>
public class DelegateCommand : ICommand
{
Func<object, bool> canExecute;
Action<object> executeAction;
bool canExecuteCache;
public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
{
this.executeAction = executeAction;
this.canExecute = canExecute;
}
#region ICommand Members
/// <summary>
/// 是否被禁用
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter)
{
bool temp = canExecute(parameter);
if (canExecuteCache != temp)
{
canExecuteCache = temp;
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, new EventArgs());
}
}
return canExecuteCache;
}
public event EventHandler CanExecuteChanged;
/// <summary>
/// 执行命令
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
{
executeAction(parameter);
}
#endregion
}
}
//在XAML里面,用interaction来绑定这个事件,而不是在button里面用command来绑定,这样做有个好处,就是非常直观,并且可以响应其他的很多事件
<Button x:Name="BTN_CM1" Content="DelegateCommand" Height="115" Width="148" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding cm1click}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
二、命令绑定方式
方式1
xaml
<Grid>
<Grid.DataContext>
<ViewModel:MyViewModel />
</Grid.DataContext>
<!--
Command - 指定需要关联的命令
CommandParameter - 传递给 Command 的参数
-->
<StackPanel >
<TextBox Name="txtName" Text="webabcd" />
<Button Content="Hello" Command="{Binding Hello}" CommandParameter="{Binding ElementName=txtName, Path=Text }" />
</StackPanel>
</Grid>
ViewModel
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());
}
}
方式2:
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>