Created
February 22, 2011 18:58
-
-
Save jgable/839154 to your computer and use it in GitHub Desktop.
ClickCommandTrigger from MVVMLight EventToCommand TriggerAction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Input; | |
using GalaSoft.MvvmLight.Command; | |
public class ClickCommandTrigger : System.Windows.Interactivity.EventTriggerBase<Button> | |
{ | |
/// <summary> | |
/// The <see cref="Command" /> dependency property's name. | |
/// </summary> | |
public const string CommandPropertyName = "Command"; | |
/// <summary> | |
/// Gets or sets the value of the <see cref="Command" /> | |
/// property. This is a dependency property. | |
/// </summary> | |
public ICommand Command | |
{ | |
get | |
{ | |
return (ICommand)GetValue(CommandProperty); | |
} | |
set | |
{ | |
SetValue(CommandProperty, value); | |
} | |
} | |
/// <summary> | |
/// Identifies the <see cref="Command" /> dependency property. | |
/// </summary> | |
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( | |
CommandPropertyName, | |
typeof(ICommand), | |
typeof(ClickCommandTrigger), | |
new PropertyMetadata(null)); | |
/// <summary> | |
/// The <see cref="CommandParameter" /> dependency property's name. | |
/// </summary> | |
public const string CommandParameterPropertyName = "CommandParameter"; | |
/// <summary> | |
/// Gets or sets the value of the <see cref="CommandParameter" /> | |
/// property. This is a dependency property. | |
/// </summary> | |
public object CommandParameter | |
{ | |
get | |
{ | |
return (object)GetValue(CommandParameterProperty); | |
} | |
set | |
{ | |
SetValue(CommandParameterProperty, value); | |
} | |
} | |
/// <summary> | |
/// Identifies the <see cref="CommandParameter" /> dependency property. | |
/// </summary> | |
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register( | |
CommandParameterPropertyName, | |
typeof(object), | |
typeof(ClickCommandTrigger), | |
new PropertyMetadata(null)); | |
private System.Windows.Interactivity.TriggerAction execAction; | |
public ClickCommandTrigger() | |
: base() | |
{ } | |
protected override void OnEvent(EventArgs eventArgs) | |
{ | |
if (execAction == null) | |
{ | |
execAction = CreateExecAction(); | |
this.Actions.Add(execAction); | |
} | |
InvokeActions(eventArgs); | |
} | |
private System.Windows.Interactivity.TriggerAction CreateExecAction() | |
{ | |
var act = new GalaSoft.MvvmLight.Command.EventToCommand(); | |
// Set the bindings for the command and parameter. | |
var cmdBind = new Binding("Command") { Source = this }; | |
var parmBind = new Binding("CommandParameter") { Source = this }; | |
BindingOperations.SetBinding(act, EventToCommand.CommandProperty, cmdBind); | |
BindingOperations.SetBinding(act, EventToCommand.CommandParameterProperty, parmBind); | |
return act; | |
} | |
protected override string GetEventName() | |
{ | |
return "Click"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment