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.

    enter image description here

  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.

你可能感兴趣的:(datagrid)