wpf-10125-controls-and-libraries-data-grid-mvvm-enhancements-examples-binding-to-a-collection-of-selected-items.md
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.
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:
<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;
}
}
}
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
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>
In Multiple Row Selection mode, the GridControl‘s DataControlBase.SelectedItems collection is populated with items in the same order rows/nodes are selected.
When the GridControl works in the Server Mode or with Virtual Sources, the DataControlBase.SelectedItems collection returns an empty list. Use the DataControlBase.GetSelectedRowHandles and DataControlBase.GetRow / DataControlBase.GetRowAsync methods to obtain row handles and data items.
When you work with the master-detail GridControl, the DataControlBase.SelectedItems collection contains items only from the master GridControl. To obtain selected detail records in a DataControlDetailDescriptor:
To get selected nodes ordered by visible indexes, use the TreeListControlBase.GetSelectedNodes method. To get row handles of selected items ordered by visible indexes, use the DataControlBase.GetSelectedRowHandles method.
See Also