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

标签 c# wpf mvvm

我正在使用 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>

以及在 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);
    }

所以在这里我需要传递多个对象,例如生成此事件的 Control、鼠标相关属性等。现在我传递单个参数,并且此代码工作正常。 所以我的问题是如何从 Xaml(View) 传递多个参数并在此 ViewModel 上访问它们。 有代码帮助吗?

最佳答案

您可以尝试使用自定义Converter和MultiBinding

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

转换器

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

参数

public class FindCommandParameters
{
  public string Property1 { get; set; }
  public string Property2 { get; set; }
}

关于c# - 如何使用 MVVM 在 WPF 应用程序中的 InvokeCommandAction 中将多个参数作为 CommandParameter 传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12601684/

相关文章:

c# - 为什么 if( !A && !B ) 不能优化为单个 TEST 指令?

c# - 从派生类对象访问重写的方法

c# - 身份服务器 : Add claims to access token in hybrid flow in MVC client

C# - 如何将特定记录从数据库显示到 ASP.NET MVC 中的 View

c# - 使用按钮更改 ViewModel 属性

wpf - TreeView 与嵌套扩展器

c# - 粗体和斜体文本取决于 WPF ComboBox 中的数据,没有 XAML

c# - 通过 WPF 发送电子邮件使应用程序关闭

c# - 加密 App.Config 部分并部署到多台机器

c# - View 模型的 MVVM 继承