如何InvokeCommandAction传递多个参数CommandParameter在WPF应用程序中使用MVVM [英] How to pass Multiple parameters as CommandParameter in InvokeCommandAction In WPF App Using MVVM

查看:7715
本文介绍了如何InvokeCommandAction传递多个参数CommandParameter在WPF应用程序中使用MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用System.Windows.interactivity.dll让我通过以下方式视图模型的鼠标事件。

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>

和在视图模型。

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



所以在这里我需要通过多个物体,如控制生成此事件,鼠标相关的属性等。现在我传递一个参数与该代码是工作的罚款。
所以我的问题是,如何在XAML(视图)将多个参数传递和访问它们在这个视图模型。
的任何代码的帮助?

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

推荐答案

我Aghilas同意。这就是它是如何做。
我在Aghilas代码改进,以澄清失踪。
指出,我:InvokeCommandAction.CommandParameter必须在invokeCommandAction声明中提出。

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>

这篇关于如何InvokeCommandAction传递多个参数CommandParameter在WPF应用程序中使用MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆