简体   繁体   中英

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

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

 <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}" 
                         CommandParameter="{Binding SelectedItem, ElementName=listBox}"/>
        </i:EventTrigger>

        </i:Interaction.Triggers>
</ListBox>

and in ViewModel.

        public class Headers
    {
        public Headers()
        {
            IsSelected = false;
        }

        public string Text { get; set; }
        public ListBox Control { get; set; }
        public bool IsSelected { get; set; }
    }
   public ObservableCollection<Headers> HeaderList
    {
        get { return _headerList; }
        set
        {
            _headerList = value;
            base.OnPropertyChanged("HeaderList");
        }

    }
 public ICommand MouseLeftButtonUpCommand { get; set; }
 public DesignTemplateViewModel()
    {
        string file = SessionHelper.FilePath;
        List<string> columns = new List<string>();


        if (!string.IsNullOrEmpty(file))
        {
            ExcelHelper Excel = new ExcelHelper(file);
            columns = Excel.GetHeader();
        }
        else
        {
            columns.Add("Name");
            columns.Add("FatherName");
            columns.Add("MotherName");
            columns.Add("Class");
            columns.Add("RollNo");
            columns.Add("ModeOfTransport");
            columns.Add("Phone");
            columns.Add("Mobile");
        }
        HeaderList = new ObservableCollection<Headers>();

        foreach (string column in columns)
        {
            HeaderList.Add(new Headers
            {
                Text = column,
            });
        }

        MouseLeftButtonUpCommand = new RelayCommand((item) => OnMouseLeftButtonUp((Headers)item));
    }
  private void OnMouseLeftButtonUp(Headers sender)
    {
        ListBox control = sender.Control as ListBox;
        DragDrop.DoDragDrop(control, sender.Text, DragDropEffects.Copy);
    }

So here I need to pass multiple objects such as Control that generated this event, Mouse related properties etc. Now I am Passing single parameter and this code is working fine. So My Questions is that how can pass Multiple parameters from Xaml(View) and access them on this ViewModel. Any code help??

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM