WPF Datagrid multiple selecteditems in MVVM

I can assure you: SelectedItems is indeed bindable as a XAML CommandParameter

After a lot of digging and googling, I have finally found a simple solution to this common issue.

To make it work you must follow ALL the following rules:

  1. Following Ed Ball‘s suggestion‘, on you XAML command databinding, define CommandParameterproperty BEFORE Command property. This a very time-consuming bug.

  2. Make sure your ICommand‘s CanExecute and Execute methods have a parameter of object type. This way you can prevent silenced cast exceptions that occurs whenever databindingCommandParameter type does not match your command method‘s parameter type.

    private bool OnDeleteSelectedItemsCanExecute(object SelectedItems)
    {

       // Your goes heres

    }

    private bool OnDeleteSelectedItemsExecute(object SelectedItems)

    {

       // Your goes heres

    }

For example, you can either send a listview/listbox‘s SelectedItems property to you ICommand methods or the listview/listbox it self. Great, isn‘t it?

Hope it prevents someone spending the huge amount of time I did to figure out how to receiveSelectedItems as CanExecute parameter.

时间: 2024-12-09 12:16:52

WPF Datagrid multiple selecteditems in MVVM的相关文章

WPF DataGrid Custommization using Style and Template

转载:http://www.codeproject.com/Articles/586132/WPF-DataGrid-Custommization-using-Style-and-Templa WPF DataGrid Custommization using Style and Template Hiren Khirsaria, 2 May 2013 CPOL customize DataGridRowHeader, DataGridColumnHeader, DataGridCell, Da

WPF DataGrid常用属性记录

WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭合DataGrid的行分组. CommitEdit:确认DataGrid的编辑完成. ExpandRowGroup:展开DataGrid的行分组. GetGroupFromItem:从具体Item中得到分组. ScrollIntoView:滚动DataGrid视图. 组件常用属性: Alternat

WPF DataGrid自定义样式

WPF DataGrid自定义样式 微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. 在DataGrid中的最高水平,你可以改变的外观和感觉,通过设置一些: Property Type Values Default AlternatingRowBackground Brush Any Brush Null Background Brush Any

wpf dataGrid 选中行 失去焦点时 的背景颜色的更改

关于 wpf dataGrid 选中行 失去焦点时 的背景颜色的更改.很简单的方式,在datagrid的resource中更改InactiveSelectionHighlightBrushKey属性的值即可. 关键代码如下: <DataGrid.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Yellow

WPF DataGrid显格式

Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL    4.83 (13 votes) 1 2 3 4 5 4.83/5 - 13 votes μ 4.83, σa 0.93 [?] Rate: Add a reason or comment to your vote: x Votes of 3 or less require a comment Using Style and Bin

WPF DataGrid 之数据绑定

1. Auto generation of columns 最简单的方法莫过于让DataGrid根据数据源中的字段自动生成列了: 根据实体类的公共属性, 能够自动生成四种类型的数据列,对应关系如下: TextBox columns for string values; CheckBox columns for boolean values; ComboBox columns for enumerable values; Hyperlink columns for Uri values; 拖个Da

WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL

In my recent codeproject article on the DataGrid I described a number of techniques for handling the updates to DataTables which are bound to the grid. These examples all worked on the assumption that you want to keep your database synchronised with

[WPF]DataGrid C#添加右键弹出选择菜单

private void dataGrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { ContextMenu context = new ContextMenu(); MenuItem item = new MenuItem(); item.Header = "点击删除该行数据"; item.Click += new RoutedEventHandler(item_Click); context.Item

编写 WPF DataGrid 列模板,实现更好的用户体验

Julie Lerman 下载代码示例 最近我在为一个客户做一些 Windows Presentation Foundation (WPF) 方面的工作. 虽然我提倡使用第三方工具,但有时也会避免使用这些工具,这样做是为了体验那些坚持使用 Visual Studio 安装附带工具的开发人员会面临什么样的难题. 祝我好运吧!我们来研究一下 WPF DataGrid. 即便有 Web 搜索的帮助和来自在线论坛的建议,仍然有一些用户体验问题花了我几天时间才解决. 将 DataGrid 列分解为成对的互