Back to Devexpress

Binding to a Collection of Selected Items

wpf-10125-controls-and-libraries-data-grid-mvvm-enhancements-examples-binding-to-a-collection-of-selected-items.md

latest6.7 KB
Original Source

Binding to a Collection of Selected Items

  • Jan 09, 2023
  • 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

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.
xaml
<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.

  • C#

  • VB.NET

cs
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;
        }
    }
}
vb
Imports System.Collections.Generic
Imports System.Linq
Imports System.Collections.ObjectModel
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations

Namespace WPFGridMVVMSelection

    Public Class CustomersViewModel
        Inherits ViewModelBase

        Public ReadOnly Property Customers As IList(Of Customer) = CustomersDataModel.GetCustomers()

        Public ReadOnly Property Selection As ObservableCollection(Of Customer) = New ObservableCollection(Of Customer)()

        <Command>
        Public Sub DeleteSelectedRows()
            Enumerable.ToList(Selection).ForEach(Sub(item) Customers.Remove(item))
        End Sub

        Public Function CanDeleteSelectedRows() As Boolean
            Return Selection.Count > 0
        End Function
    End Class
End Namespace

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:

xaml
<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

Row Selection