简体   繁体   English

如何使用MVVM在WPF应用程序中的InvokeCommandAction中将多个参数作为CommandParameter传递

[英]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. 我正在以以下方式使用System.Windows.interactivity.dll在ViewModel中获取鼠标事件。

 <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. 和在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. 所以我的问题是如何从Xaml(View)传递多个参数并在此ViewModel上访问它们。 Any code help?? 任何代码帮助吗?

You can try with custom Converter and MultiBinding 您可以尝试使用自定义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. 我改进了Aghilas的代码,以澄清缺少的内容。 note that "i:InvokeCommandAction.CommandParameter" must be put inside the invokeCommandAction declaration. 请注意,“ i:InvokeCommandAction.CommandParameter”必须放在invokeCommandAction声明中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows Phone的InvokeCommandAction中将多个参数作为CommandParameter传递 - How to pass Multiple parameters as CommandParameter in InvokeCommandAction in Windows phone 通过 InvokeCommandAction 使用多个参数时如何发送 EventArgs? - How to send EventArgs when using multiple parameters through InvokeCommandAction? 如何将WPF CommandParameter带到MVVM中的ViewModel? - How to take a WPF CommandParameter to the ViewModel in MVVM? C#和WPF,当使用mvvm样式以及将按钮动作用作带有commandparameter的命令时,如何始终在commandparameter中创建一个新对象? - C# and WPF, when using mvvm style, and button action as command with commandparameter, how to ALWAYS create a new object in commandparameter? 如何使用InvokeCommandAction调用我的方法并传入参数? - How do I go about using InvokeCommandAction to call a method of mine and pass in parameters? 从WPF MVVM应用程序将参数传递到WPF用户控制库 - Pass parameters to WPF User Control Library from WPF MVVM app 如何在WPF XAML中将控件的HashCode作为CommandParameter传递 - How to Pass HashCode of a Control as a CommandParameter in WPF XAML InvokeCommandAction CommandParameter始终为零 - InvokeCommandAction CommandParameter always zero CommandParameter始终为空wpf MVVM - CommandParameter always empty wpf MVVM CommandParameter如何在MVVM中工作? - How CommandParameter works in MVVM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM