Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Binding to a Collection of Selected Items

  • 3 minutes to read

This topic describes how to bind the GridControl‘s selection to a collection of items defined in a Model or View Model. You also will learn how to bind a control to the GridControl‘s selection.

#Bind GridControl Selection to a Collection in a View Model

DevExpress WPF | Grid Control - Delete Selected Rows Command

View Example: WPF Data Grid - Bind Selected Rows to a ViewModel Property

The following code sample shows how to synchronize the GridControl‘s selection with an item collection in a View Model. The code sample:

  1. Binds the GridControl‘s SelectedItems property to a Selection collection defined in a View Model.
  2. Adds a button that deletes selected rows.
<Window.DataContext>
    <local:CustomersViewModel/>
</Window.DataContext>
<Button Command="{Binding DeleteSelectedRowsCommand}" Content="Delete Selected Rows"/>
<dxg:GridControl ItemsSource="{Binding Customers}" 
                 SelectionMode="Row" 
                 SelectedItems="{Binding Selection}"
                 ... />

The view model includes the following classes and collections:

  • Customer - a data object that contains customer information (name, city, number of visits, birthday).
  • CustomerDataModel - the customer data model that populates the Customers collection with data.
  • CustomersViewModel - the customer view model.
  • Customers - a collection of customers displayed in the GridControl.
  • Selection - a collection of GridControl selected items.
  • DeleteSelectedRows - the command that deletes selected items from the GridControl.
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;

namespace WPFGridMVVMSelection {
    public class CustomersViewModel : ViewModelBase {
        public IList<Customer> Customers { get; } = CustomersDataModel.GetCustomers();
        public ObservableCollection<Customer> Selection { get; } = new ObservableCollection<Customer>();

        [Command]
        public void DeleteSelectedRows() {
            Selection.ToList().ForEach(item => Customers.Remove(item));
        }
        public bool CanDeleteSelectedRows() {
            return Selection.Count > 0;
        }
    }
}

#Bind ListBoxEdit Items to GridControl Selection

The following code sample binds the ListBoxEdit to the DataControlBase.SelectedItems collection to show the GridControl‘s selected rows in the ListBox:

<dxg:GridControl x:Name="grid" SelectionMode="Row" ... />
<GroupBox Header="Selected Records">
    <dxe:ListBoxEdit ItemsSource="{Binding SelectedItems, ElementName=grid}"
                     DisplayMember="Name"/>
</GroupBox>

#Notes and Limitations

See Also