WPF 使用Command,CommandParameter向 MVVM传递多个参数

有的时候需要向ViewModel传递两个CommandParameter参数。需要实现一个Converter。然后就可以实现多个参数传递啦。本案例以MVVM为基础做的Demo。还有一个编写的按钮样式的Style。可已给需要的同学做个参考。工程代码放在最后了。好了。看下如何传递参数的吧,如下图。(obj中已经传递过来两个参数。一个是打开的窗口。负责逻辑处理后关闭窗口,一个是datagrid。用于操作DataGrid控件。)

WPF 使用Command,CommandParameter向 MVVM传递多个参数_第1张图片

工程代码:

WPF 使用Command,CommandParameter向 MVVM传递多个参数_第2张图片

 核心代码:


    
        
    
    
        
            
            
        
        
            
                
                
                
                
            
        
        
            
            
        
    

 ObjectConvert.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace DataGridBinding
{
    /// 
    /// CommandParameter 多参数传递
    /// 
    public class ObjectConvert : IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public static object ConverterObject;

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            return values.ToArray();
        }

        public object[] ConvertBack(object value, Type[] targetTypes,
          object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

ViewModel中ConfirmExecute函数已经获得多个参数,

 public void ConfirmExecute(object obj)
        {
            object[] multiObj = obj as object[];
            Window win = multiObj[0] as Window;
            DataGrid dataGrid = multiObj[1] as DataGrid;
            if(dataGrid != null)

            if (win != null)
                win.Close();
        }
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using DataGridBinding.Model;
using GalaSoft.MvvmLight.Command;
using System.Windows;
using System.Windows.Controls;

namespace DataGridBinding.ViewModel
{
    public class DataGridVM
    {
        private List _infoList;

        public List InfoList { get => _infoList; set => _infoList = value; }
        public ICommand ConfirmCommand { get; set; }
        public ICommand CancelCommand { get; set; }

        public DataGridVM()
        {

            ConfirmCommand = new RelayCommand(ConfirmExecute, CanConfirm);
            CancelCommand = new RelayCommand(CancelConfirm, Canfirm);
            InfoList = new List();
            InfoList.Add(new InfoModel() { Index = "0", Name = "张三", Age = "41", BirthDay = "1980-03-01" });
            InfoList.Add(new InfoModel() { Index = "1", Name = "李四", Age = "29", BirthDay = "1992-05-04" });
            InfoList.Add(new InfoModel() { Index = "2", Name = "王五", Age = "19", BirthDay = "2002-01-28" });
            InfoList.Add(new InfoModel() { Index = "3", Name = "赵强", Age = "22", BirthDay = "1999-08-31" });
            InfoList.Add(new InfoModel() { Index = "4", Name = "王芳", Age = "35", BirthDay = "1986-05-23" });
        }

        public void ConfirmExecute(object obj)
        {
            object[] multiObj = obj as object[];
            Window win = multiObj[0] as Window;
            DataGrid dataGrid = multiObj[1] as DataGrid;
            if(dataGrid != null)

            if (win != null)
                win.Close();
        }

        private bool CanConfirm(object ojb)
        {
            return true;
        }

        public void CancelConfirm(object obj)
        {

        }

        private bool Canfirm(object obj)
        {
            return true;
        }
    }
}

资源地址:https://download.csdn.net/download/chulijun3107/20672684

你可能感兴趣的:(MVVM,DataGrid,WPF)