编辑:我应该更好地阅读这个问题。绑定表达式的默认根是被绑定的 UI 元素的 DataContext,因此绑定表达式将 VM.SomeCommand 读取为 DataContext.VM.SomeCommand。 VM.SomeCommand 应该更改为只是 SomeCommand,因为您的视图的 DataContext 应该是您的 ViewModel。
Expression Blend 交互性框架不支持将事件参数传递给命令。我建议使用 MVVM 框架。
使用 Bradley Uffner 提到的 MVVM Light,您可以这样做:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove">
<cmd:EventToCommand Command="{Binding Mode=OneWay, Path=SomeCommand}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
但是,如果您出于某种原因反对该想法,您可以尝试here 讨论的解决方案。
链接中的示例代码:
public sealed class InvokeDelegateCommandAction : TriggerAction<DependencyObject>
{
/// <summary>
///
/// </summary>
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(InvokeDelegateCommandAction), null);
/// <summary>
///
/// </summary>
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command", typeof(ICommand), typeof(InvokeDelegateCommandAction), null);
/// <summary>
///
/// </summary>
public static readonly DependencyProperty InvokeParameterProperty = DependencyProperty.Register(
"InvokeParameter", typeof(object), typeof(InvokeDelegateCommandAction), null);
private string commandName;
/// <summary>
///
/// </summary>
public object InvokeParameter
{
get
{
return this.GetValue(InvokeParameterProperty);
}
set
{
this.SetValue(InvokeParameterProperty, value);
}
}
/// <summary>
///
/// </summary>
public ICommand Command
{
get
{
return (ICommand)this.GetValue(CommandProperty);
}
set
{
this.SetValue(CommandProperty, value);
}
}
/// <summary>
///
/// </summary>
public string CommandName
{
get
{
return this.commandName;
}
set
{
if (this.CommandName != value)
{
this.commandName = value;
}
}
}
/// <summary>
///
/// </summary>
public object CommandParameter
{
get
{
return this.GetValue(CommandParameterProperty);
}
set
{
this.SetValue(CommandParameterProperty, value);
}
}
/// <summary>
///
/// </summary>
/// <param name="parameter"></param>
protected override void Invoke(object parameter)
{
this.InvokeParameter = parameter;
if (this.AssociatedObject != null)
{
ICommand command = this.ResolveCommand();
if ((command != null) && command.CanExecute(this.CommandParameter))
{
command.Execute(this.CommandParameter);
}
}
}
private ICommand ResolveCommand()
{
ICommand command = null;
if (this.Command != null)
{
return this.Command;
}
var frameworkElement = this.AssociatedObject as FrameworkElement;
if (frameworkElement != null)
{
object dataContext = frameworkElement.DataContext;
if (dataContext != null)
{
PropertyInfo commandPropertyInfo = dataContext
.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(
p =>
typeof(ICommand).IsAssignableFrom(p.PropertyType) &&
string.Equals(p.Name, this.CommandName, StringComparison.Ordinal)
);
if (commandPropertyInfo != null)
{
command = (ICommand)commandPropertyInfo.GetValue(dataContext, null);
}
}
}
return command;
}
}
来自博客的示例用法:
<ComboBox>
<ComboBoxItem Content="Foo option 1" />
<ComboBoxItem Content="Foo option 2" />
<ComboBoxItem Content="Foo option 3" />
<Interactivity:Interaction.Triggers>
<Interactivity:EventTrigger EventName="SelectionChanged" >
<Presentation:InvokeDelegateCommandAction
Command="{Binding SubmitFormCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=InvokeParameter}" />
</Interactivity:EventTrigger>
</Interactivity:Interaction.Triggers>
</ComboBox>
注意:我尚未测试此博客中的解决方案。