Question

I'm using the Interaction dll and I want to "call" a method from the ViewModel and to pass her the mouse Args and another params.

I have tried to use the Microsoft.Expression.Interaction with the CallMethod like this:

<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>

But my problem is that the method networkControl_MouseDown need to use a parameter that is a view parameter, so I thought about passing this parameter as an argument to the function and I read that the CallMethodAction does not support argument passing.

Is there any solution to my problem?

Was it helpful?

Solution

You could use the MvvmLight toolkit. It has EventToCommand behavior wich has the property PassEventArgsToCommand.

Edit: Unfortunately you can only use one way or the other:

<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>

If you realy need to pass the EventArgs and additional parameters you would need to implement your own trigger. The source of Mvvm Light EventToCommand http://mvvmlight.codeplex.com/SourceControl/latest#GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET35)/Command/EventToCommand.cs should get you started. Link not working: look for GalaSoft.MvvmLight -> Extas(NET35) -> Commands.

Edit: For using it with 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();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top