Non c'è modo integrato, ecco come lo faccio:
Il trigger Interaction classico è usato in questo modo:
<Button Content="I am a button">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<i:InvokeCommandAction Command="{Binding CommandWithNoArgs}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
Non possiamo accedere al EventArgs
del MouseEnter
evento attraverso l'associazione, quindi dovremo modificare il pezzo che lo getta via.
In questo caso, il pezzo è il InvokeCommandAction
.
"Quindi andiamo a suddividerla e ignorare un metodo conveniente che ci aspettava da sempre" è quello che mi sarebbe piaciuto scrivere. Ma la classe è sigillata.
così stiamo andando ad avere per creare una sottoclasse suo genitore (abstract) classe: TriggerAction<DependencyObject>
L'implementazione di base è:
public class InteractiveCommand : TriggerAction<DependencyObject>
{
protected override void Invoke(object parameter)
{
}
}
E questo è il vostro parameter
EventArgs
!
Ma resisti, non è così semplice, dobbiamo riprodurre il comportamento di un normale InvokeCommandAction
.
Tramite Reflector, l'ho decompilato (ma potresti andare a dare un'occhiata alla fonte ufficiale, sono solo pigro).
Non ci occuperemo della proprietà di dipendenza CommandParameter
, supponiamo che se si utilizza questa invece di InvokeCommandAction
, in realtà si desidera il EventArgs
ogni volta.
Qui vale la classe completo (WPF solo, vedi EDIT per Silverlight):
public class InteractiveCommand : TriggerAction<DependencyObject>
{
protected override void Invoke(object parameter)
{
if (base.AssociatedObject != null)
{
ICommand command = this.ResolveCommand();
if ((command != null) && command.CanExecute(parameter))
{
command.Execute(parameter);
}
}
}
private ICommand ResolveCommand()
{
ICommand command = null;
if (this.Command != null)
{
return this.Command;
}
if (base.AssociatedObject != null)
{
foreach (PropertyInfo info in base.AssociatedObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (typeof(ICommand).IsAssignableFrom(info.PropertyType) && string.Equals(info.Name, this.CommandName, StringComparison.Ordinal))
{
command = (ICommand)info.GetValue(base.AssociatedObject, null);
}
}
}
return command;
}
private string commandName;
public string CommandName
{
get
{
base.ReadPreamble();
return this.commandName;
}
set
{
if (this.CommandName != value)
{
base.WritePreamble();
this.commandName = value;
base.WritePostscript();
}
}
}
#region Command
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
// Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(InteractiveCommand), new UIPropertyMetadata(null));
#endregion
}
Come con qualsiasi codice si recupera da Internet, io lo consigliamo vivamente la lettura attraverso tutta la classe, e cercando di capire cosa fa. Non limitarti a lanciarlo nella tua app.
Ora possiamo fare:
<Button Content="I am a button">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<local:InteractiveCommand Command="{Binding CommandWithEventArgs}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
E il codice dietro:
#region CommandWithEventArgs
DelegateCommand<MouseEventArgs> _CommandWithEventArgs;
/// <summary>
/// Exposes <see cref="CommandWithEventArgs(MouseEventArgs)"/>.
/// </summary>
public DelegateCommand<MouseEventArgs> CommandWithEventArgs
{
get { return _CommandWithEventArgs ?? (_CommandWithEventArgs = new DelegateCommand<MouseEventArgs>(CommandWithEventArgs)); }
}
#endregion
public void CommandWithEventArgs(MouseEventArgs param)
{
}
E questo è un wrap;)
EDIT: per Silverlight, utilizzare questo codice invece:
public class InteractiveCommand : TriggerAction<DependencyObject>
{
protected override void Invoke(object parameter)
{
if (base.AssociatedObject != null)
{
ICommand command = Command;
if ((command != null) && command.CanExecute(parameter))
{
command.Execute(parameter);
}
}
}
#region Command
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
// Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(InteractiveCommand), new UIPropertyMetadata(null));
#endregion
}
Ma si ricorda che è meno sicuro rispetto all'utilizzo della versione completa WPF (non controlla i tipi, può bloccarsi con elementi congelati).
Non inserire tag nel titolo della domanda. Vedi [questo link] (http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title) per scrivere buoni titoli;) –