This example shows how to synchronize the GridControl‘s selection with an item collection in a ViewModel. The example:
- Binds the GridControl‘s SelectedItems property to the Selection collection defined in a ViewModel.
- Adds a button that deletes the selected rows.
Files to Look At
- MainWindow.xaml (VB: MainWindow.xaml)
- CustomersViewModel.cs (VB: CustomersViewModel.vb)
- CustomersDataModel.cs (VB: CustomersDataModel.vb)
Documentation
- Binding to a Collection of Selected Items
- DataControlBase.SelectedItems
- Multiple Row Selection
- Focus, Navigation, Selection
- WPF Data Grid: MVVM Support
More Examples
- WPF Data Grid - Handle Row Double-clicks in a MVVM Application
- How to Bind the ChartControl to the GridControl's VisibleItems Collection
- WPF Data Grid - How to Change the Appearance of Selected Cells
Example Code
XAML<Window x:Class="WPFGridMVVMSelection.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:local="clr-namespace:WPFGridMVVMSelection"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:CustomersViewModel/>
</Window.DataContext>
<DockPanel>
<Button Command="{Binding DeleteSelectedRowsCommand}" Content="Delete Selected Rows" DockPanel.Dock="Bottom"/>
<dxg:GridControl Name="grid"
AutoGenerateColumns="AddNew"
ItemsSource="{Binding Customers}"
SelectionMode="Row"
SelectedItems="{Binding Selection}">
<dxg:GridControl.View>
<dxg:TableView Name="view"/>
</dxg:GridControl.View>
</dxg:GridControl>
</DockPanel>
</Window>
C#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;
}
}
}
C#using DevExpress.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace WPFGridMVVMSelection {
public class Customer : BindableBase {
public string Name
{
get { return GetValue<string>(); }
set { SetValue(value); }
}
public string City
{
get { return GetValue<string>(); }
set { SetValue(value); }
}
public int Visits
{
get { return GetValue<int>(); }
set { SetValue(value); }
}
public DateTime? Birthday
{
get { return GetValue<DateTime?>(); }
set { SetValue(value); }
}
}
public class CustomersDataModel {
public static IList<Customer> GetCustomers() {
ObservableCollection<Customer> people = new ObservableCollection<Customer> {
new Customer() { Name = "Gregory S. Price", City = "Huntington", Visits = 4, Birthday = new DateTime(1980, 1, 1) },
new Customer() { Name = "Irma R. Marshall", City = "Hong Kong", Visits = 2, Birthday = new DateTime(1966, 4, 15) },
new Customer() { Name = "John C. Powell", City = "Luogosano", Visits = 6, Birthday = new DateTime(1982, 3, 11) },
new Customer() { Name = "Christian P. Laclair", City = "Clifton", Visits = 11, Birthday = new DateTime(1977, 12, 5) },
new Customer() { Name = "Karen J. Kelly", City = "Madrid", Visits = 8, Birthday = new DateTime(1956, 9, 5) },
new Customer() { Name = "Brian C. Cowling", City = "Los Angeles", Visits = 5, Birthday = new DateTime(1990, 2, 27) },
new Customer() { Name = "Thomas C. Dawson", City = "Rio de Janeiro", Visits = 21, Birthday = new DateTime(1965, 5, 5) },
new Customer() { Name = "Angel M. Wilson", City = "Selent", Visits = 8, Birthday = new DateTime(1987, 11, 9) },
new Customer() { Name = "Winston C. Smith", City = "London", Visits = 1, Birthday = new DateTime(1949, 6, 18) },
new Customer() { Name = "Harold S. Brandes", City = "Bangkok", Visits = 3, Birthday = new DateTime(1989, 1, 8) },
new Customer() { Name = "Michael S. Blevins", City = "Harmstorf", Visits = 4, Birthday = new DateTime(1972, 9, 14) },
new Customer() { Name = "Jan K. Sisk", City = "Naples", Visits = 6, Birthday = new DateTime(1989, 5, 7) },
new Customer() { Name = "Sidney L. Holder", City = "Watauga", Visits = 19, Birthday = new DateTime(1971, 10, 3) }
};
return people;
}
}
}