How to pass Multiple parameters as CommandParameter in InvokeCommandAction In WPF App Using MVVM

前端 未结 2 2047
别跟我提以往
别跟我提以往 2020-12-10 21:58

I am using System.Windows.interactivity.dll to get mouse events in my ViewModel in the following manner.

 

        
相关标签:
2条回答
  • 2020-12-10 22:30

    I Agree with Aghilas. That's how it's done. I improved upon Aghilas's code to clarify what was missing. note that "i:InvokeCommandAction.CommandParameter" must be put inside the invokeCommandAction declaration.

        <ListBox x:Name="listBox" ItemsSource="{Binding HeaderList}" DisplayMemberPath="Text" Width="Auto"   Margin="0,0,0,300" Height="Auto"  >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftButtonUp">
                    <i:InvokeCommandAction Command="{Binding MouseLeftButtonUpCommand}">
                        <i:InvokeCommandAction.CommandParameter>
                            <MultiBinding Converter="{StaticResource XAMLResourceAddConverter}">
                                <Binding ElementName="listBox" Path="SelectedItem"/>
                                <Binding ElementName="listBox" Path="SelectedItem"/>
                            </MultiBinding>
                        </i:InvokeCommandAction.CommandParameter>
                    </i:InvokeCommandAction>
                </i:EventTrigger>
    
            </i:Interaction.Triggers>
        </ListBox>
    
    0 讨论(0)
  • 2020-12-10 22:35

    You can try with custom Converter and MultiBinding

    <CommandParameter>
          <MultiBinding Converter="{StaticResource CustomConverter}">
           <Binding ElementName=".." Path=".."/>
           <Binding ElementName=".." Path=".."/>
          </MultiBinding>
    </CommandParameter>
    

    Converter

    class CustomConverter : IMultiValueConverter 
    {
        public object Convert (object[] Values, Type Target_Type, object Parameter, CultureInfo culture) 
        {
            var findCommandParameters = new FindCommandParameters();
            findCommandParameters.Property1 = (string)values[0];
            findCommandParameters.Property1 = (string)values[1];
            return findCommandParameters;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,   System.Globalization.CultureInfo culture)
        {
           throw new NotImplementedException();
        }
    }
    

    Parameters

    public class FindCommandParameters
    {
      public string Property1 { get; set; }
      public string Property2 { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题