有的时候需要向ViewModel传递两个CommandParameter参数。需要实现一个Converter。然后就可以实现多个参数传递啦。本案例以MVVM为基础做的Demo。还有一个编写的按钮样式的Style。可已给需要的同学做个参考。工程代码放在最后了。好了。看下如何传递参数的吧,如下图。(obj中已经传递过来两个参数。一个是打开的窗口。负责逻辑处理后关闭窗口,一个是datagrid。用于操作DataGrid控件。)
工程代码:
核心代码:
<Window x:Class="DataGridBinding.View.InfoWin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridBinding.View"
xmlns:Command="http://www.galasoft.ch/mvvmlight"
xmlns:data ="clr-namespace:DataGridBinding"
mc:Ignorable="d"
Title="InfoWin" Height="400" Width="500" ResizeMode="NoResize" x:Name="InfoWindows">
<Window.Resources>
<data:ObjectConvert x:Key="objectConverter" ></data:ObjectConvert>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<DataGrid Name="datagrid" ItemsSource="{Binding InfoList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="索引" Width="*" Binding="{Binding Index}"></DataGridTextColumn>
<DataGridTextColumn Header="姓名" Width="*" Binding="{Binding Name}"></DataGridTextColumn>
<DataGridTextColumn Header="年龄" Width="*" Binding="{Binding Age}"></DataGridTextColumn>
<DataGridTextColumn Header="生日" Width="*" Binding="{Binding BirthDay}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="确定" Width="50" Margin="2" Style="{StaticResource SimpleButton}" Command="{Binding ConfirmCommand}" >
<Button.CommandParameter>
<MultiBinding Converter="{ StaticResource ResourceKey=objectConverter}">
<Binding ElementName="InfoWindows"></Binding>
<Binding ElementName="datagrid"></Binding>
</MultiBinding>
</Button.CommandParameter>
</Button>
<Button Content="取消" Width="50" Margin="2" Style="{StaticResource SimpleButton}" Command="{Binding CancelCommand}"></Button>
</StackPanel>
</Grid>
</Window>
ObjectConvert.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace DataGridBinding
{
/// <summary>
/// CommandParameter 多参数传递
/// </summary>
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<InfoModel> _infoList;
public List<InfoModel> 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<InfoModel>();
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