Example E3139
Visible to All Users

WPF Data Grid - Bind Selected Rows to a ViewModel Property

This example shows how to synchronize the GridControl‘s selection with an item collection in a ViewModel. The example:

  1. Binds the GridControl‘s SelectedItems property to the Selection collection defined in a ViewModel.
  2. Adds a button that deletes the selected rows.

Files to Look At

Documentation

More Examples

Example Code

GridMVVMSelection/MainWindow.xaml
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>
GridMVVMSelection/CustomersViewModel.cs(vb)
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; } } }
GridMVVMSelection/CustomersDataModel.cs(vb)
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; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.