我正在使用Interaction
dll,我想从ViewModel“调用”一个方法,并将鼠标传给Args和另一个参数。
我尝试将Microsoft.Expression.Interaction
与CallMethod一起使用,如下所示:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown" >
<!--This is used in order to pass eventArgs to func-->
<ei:CallMethodAction MethodName="networkControl_MouseDown" TargetObject="{Binding}" />
</i:EventTrigger>
但我的问题是方法networkControl_MouseDown
需要使用一个参数作为视图参数,所以我考虑将此参数作为参数传递给函数,我读到CallMethodAction
不支持论证传递。
我的问题有解决办法吗?
答案 0 :(得分:0)
您可以使用MvvmLight toolkit。它具有EventToCommand行为,具有属性PassEventArgsToCommand。
编辑:不幸的是,你只能使用这种或那种方式:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:conv="clr-namespace:WpfApplication1"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cmd ="http://www.galasoft.ch/mvvmlight">
<Window.Resources>
<conv:MultiValueConverter x:Key="MultiValueConverter" />
</Window.Resources>
<Grid>
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding TestCommand}">
<!--You can only use EventArgs or CommandParameter. -->
<!--<cmd:EventToCommand.CommandParameter>
<MultiBinding Converter="{StaticResource MultiValueConverter}">
<Binding Path="Parameter1" Mode="OneWay"/>
<Binding Path="Parameter2" Mode="OneWay"/>
</MultiBinding>
</cmd:EventToCommand.CommandParameter>-->
</cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</Window>
如果您真的需要传递EventArgs和其他参数,则需要实现自己的触发器。 Mvvm Light EventToCommand http://mvvmlight.codeplex.com/SourceControl/latest#GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET35)/Command/EventToCommand.cs的来源应该让你入门。 链接不工作:寻找GalaSoft.MvvmLight - &gt; Extas(NET35) - &gt;命令。
编辑:用于MultiBinding:
public ICommand TestCommand
{
get
{
if(_testCommand == null)
{
_testCommand = new RelayCommand(OnTestCommand); //ICommand implementation
}
return _testCommand;
}
}
public void OnTestCommand(object args)
{
var array = (object[])args;
var p1 = array[0];
var p2 = array[1];
}
public class MultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//Important. Otherwise values in execute method of command will be null.
return values.ToArray();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}