WPF Command绑定并传参(以DataGrid示例)


一、问题场景:

  使用WPF的DataGrid来展示表格数据,想要批量删除或者导出数据行时,由于SelectedItems属性不支持MVVM的方式绑定(该属性是只读属性),所以可以通过命令参数的方式将该属性值传给命令,即利用CommandParameter将SelectedItems传递给删除或导出命令。

二、使用方式:

1.xaml部分

<DataGrid x:Name="dtgResult" ItemsSource="{Binding ResultInfo}" CanUserSortColumns="True" Height="205" Width="866" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="患者ID" Binding="{Binding PatientId}" MinWidth="46" IsReadOnly="True"/>
        <DataGridTextColumn Header="姓名" Binding="{Binding PatientName}" MinWidth="46" IsReadOnly="True"/>
        <DataGridTextColumn Header="性别" Binding="{Binding PatientGender}" MinWidth="46" IsReadOnly="True"/>
    </DataGrid.Columns>
</DataGrid>
<Button x:Name="btnResultDel" Content="删除" Command="{Binding ResultDeleteCommand}" CommandParameter="{Binding SelectedItems,ElementName=dtgResult}"/>

2.C#部分

private RelayCommand _deleteCommand;
public ICommand ResultDeleteCommand {
  get
  {
    
if (_deleteCommand == null)     {
      _deleteCommand
= new RelayCommand(param =>On_Delete_Command_Excuted(param));     }   return _deleteCommand;   } } private void On_Delete_Command_Excuted(Object param) {

}

每次触发ResultDeleteCommand,都会把控件dtgResult的SelectedItems属性值作为参数param传递给On_Delete_Command_Excuted方法。

三、Tip

1.如何把Object类型的参数转成List<T>呢? (其中T是DataGrid中每条数据的类型)

System.Collections.IList items = (System.Collections.IList)param;
var collection = items.Cast<T>();
var selectedItems = collection.ToList();

其中selectedItems就是用户在DataGrid界面选中的多条数据行。

2.RelayCommand

/// <summary>
/// A command whose sole purpose is to relay its functionality to other objects by invoking delegates. The default return value for the CanExecute method is 'true'.
/// </summary>
public class RelayCommand : ICommand
{
    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;        

    /// <summary>
    /// Creates a new command that can always execute.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    public RelayCommand(Action<object> execute): this(execute, null)
    {
    }

    /// <summary>
    /// Creates a new command.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
         if (execute == null)
            throw new ArgumentNullException("execute");
_execute
= execute; _canExecute = canExecute; } public bool CanExecute(object parameter) { return _canExecute == null ? true : _canExecute(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { _execute(parameter); } }

 

引用来源:https://www.cnblogs.com/LXLR/p/15770040.html

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
管理员
上一篇:C# 设置或验证 PDF中的文本域格式
下一篇:初识MASA Blazor
评论列表

发表评论

评论内容
昵称:
关联文章

WPF CommandDataGrid示例
WPF_15_格式化的数据
数据模式
WPF 双向到非公开 set 方法属性在 NET 45 和 NET Core 行为的不同
支付宝支付报错:40003,三方应用未服务商账号
WPF DataGrid 如何将被选中行带到视野中
C# NPOI导出excel列的下拉数据源
走进WPF之MVVM完整案例
从一次解决Nancy参数“bug”开始发布自己的第一个nuget包(下篇)
YESWEB开发框架,账套域名
从一次解决Nancy参数“bug”开始发布自己的第一个nuget包(上篇)
使用.NET 6开发TodoList应用(26)——实现Configuration和Option的强类型
简单示例
C# 值得永久收藏的WPF项目实战(经典)
winform对象数据源
WPF对象级资源
WPF程序级资源
C# XML 序列化与反序列化详解及实战示例
C# WPF MVVM模式Prism框架从零搭建(经典)
html+js上文件

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码