DataControlBase.SelectedItems Property
Gets or sets data objects that correspond to rows (or nodes in TreeListView) currently selected within a View.
Namespace: DevExpress.Xpf.Grid
Assembly: DevExpress.Xpf.Grid.v19.2.Core.dll
Declaration
public IList SelectedItems { get; set; }
Public Property SelectedItems As IList
Property Value
Type |
Description |
IList |
The list which contains data objects corresponding to selected rows.
|
A collection that is bound to the SelectedItems property should be initialized.
If the grid is in Server Mode, the SelectedItems property always returns an empty list.
Refer to the Row Selection topic for more information.
Note
When engineering a WPF application using the Model View ViewModel (MVVM) architectural pattern, you may be required to specify selected items in a Model or ViewModel. In this instance, a collection of selected items should be initialized in the Model or View Model before it is bound to the grid’s SelectedItems property. Otherwise, it will not be populated with items selected within the grid.
Example
This example demonstrates how to bind GridControl’s selected rows to a property in ViewModel in a MVVM-based application. The SelectionAttachedBehavior helper class used in this sample, provides a bindable SelectedItemsSource property, that can be used to define selection at the ViewModel level.
Note, we made the SelectionAttachedBehavior class as generic as possible, and the same approach can also be used for other controls that support multiple selection. This example demonstrates how this can be done when working with the standard ListBox control, as well as with the standard DataGrid.
<Window x:Class="WPFGridMVVMSelection.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<dxg:GridControl ItemsSource="{Binding Source}" AutoGenerateColumns="AddNew" SelectionMode="Row" SelectedItems="{Binding Selection}" >
<dxg:GridControl.View>
<dxg:TableView NavigationStyle="Row" />
</dxg:GridControl.View>
</dxg:GridControl>
<ListView SelectionMode="Multiple" Grid.Column="1" ItemsSource="{Binding Source}"
dx:SelectionAttachedBehavior.SelectedItemsSource="{Binding Selection}" />
<dxg:GridControl Grid.Column="2" ItemsSource="{Binding Source}" AutoGenerateColumns="AddNew" SelectionMode="Row" SelectedItems="{Binding Selection}">
<dxg:GridControl.View>
<dxg:TableView NavigationStyle="Row" />
</dxg:GridControl.View>
</dxg:GridControl>
<dxg:GridControl Grid.Column="3" ItemsSource="{Binding Source}" AutoGenerateColumns="AddNew" SelectionMode="Row" SelectedItems="{Binding Selection}">
<dxg:GridControl.View>
<dxg:CardView NavigationStyle="Row" />
</dxg:GridControl.View>
</dxg:GridControl>
<Button Grid.Row="1" Grid.ColumnSpan="2" Content="Select Odd Rows" Command="{Binding SelectOddRowsCommand}"/>
<Button Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Content="Delete Selected Rows" Command="{Binding DeleteSelectedRowsCommand}" />
</Grid>
</Window>
using System.Windows;
using Model;
namespace WPFGridMVVMSelection {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
DataContext = new ViewModel();
}
}
}
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using DevExpress.Utils.Commands;
using DevExpress.Xpf.Core.Commands;
using System.Collections;
using System.Linq;
using DevExpress.Xpf.Core;
using System.Collections.Specialized;
namespace Model {
public class ViewModel {
private readonly ObservableCollection<DataItem> source = DataItem.GetData();
private readonly ObservableCollection<DataItem> selection = new ObservableCollection<DataItem>();
public ObservableCollection<DataItem> Source { get { return this.source; } }
public ObservableCollection<DataItem> Selection { get { return this.selection; } }
public ICommand SelectOddRowsCommand { get; private set; }
public DelegateCommand<object> DeleteSelectedRowsCommand { get; private set; }
public ViewModel() {
Selection.CollectionChanged += Selection_CollectionChanged;
SelectOddRowsCommand = new DelegateCommand<object>(SelectOddRows);
DeleteSelectedRowsCommand = new DelegateCommand<object>(DeleteSelectedRows, CanDeleteSelectedRows);
}
void Selection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
DeleteSelectedRowsCommand.RaiseCanExecuteChanged();
}
private void SelectOddRows(object obj) {
Selection.Clear();
for(int i = 0; i < Source.Count; i += 2)
Selection.Add(Source[i]);
}
private void DeleteSelectedRows(object obj) {
Selection.ToList().ForEach(item => Source.Remove(item));
}
private bool CanDeleteSelectedRows(object obj) {
return Selection.Count > 0;
}
}
public class DataItem {
public static ObservableCollection<DataItem> GetData(int count = 100) {
ObservableCollection<DataItem> result = new ObservableCollection<DataItem>();
for(int i = 0; i < count; i++)
result.Add(new DataItem(i, "Name" + i, DateTime.Now.AddDays(i)));
return result;
}
public int Id { get; set; }
public string Name { get; set; }
public DateTime Date_Time { get; set; }
public DataItem() { }
public DataItem(int id, string name, DateTime date) {
Id = id;
Name = name;
Date_Time = date;
}
public override string ToString() {
return Name;
}
}
}
Imports System.Windows
Imports Model
Namespace WPFGridMVVMSelection
''' <summary>
''' Interaction logic for MainWindow.xaml
''' </summary>
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
DataContext = New ViewModel()
End Sub
End Class
End Namespace
Imports System
Imports System.Collections.ObjectModel
Imports System.Windows.Input
Imports DevExpress.Utils.Commands
Imports DevExpress.Xpf.Core.Commands
Imports System.Collections
Imports System.Linq
Imports DevExpress.Xpf.Core
Imports System.Collections.Specialized
Namespace Model
Public Class ViewModel
Private ReadOnly source_Renamed As ObservableCollection(Of DataItem) = DataItem.GetData()
Private ReadOnly selection_Renamed As New ObservableCollection(Of DataItem)()
Public ReadOnly Property Source() As ObservableCollection(Of DataItem)
Get
Return Me.source_Renamed
End Get
End Property
Public ReadOnly Property Selection() As ObservableCollection(Of DataItem)
Get
Return Me.selection_Renamed
End Get
End Property
Private privateSelectOddRowsCommand As ICommand
Public Property SelectOddRowsCommand() As ICommand
Get
Return privateSelectOddRowsCommand
End Get
Private Set(ByVal value As ICommand)
privateSelectOddRowsCommand = value
End Set
End Property
Private privateDeleteSelectedRowsCommand As DelegateCommand(Of Object)
Public Property DeleteSelectedRowsCommand() As DelegateCommand(Of Object)
Get
Return privateDeleteSelectedRowsCommand
End Get
Private Set(ByVal value As DelegateCommand(Of Object))
privateDeleteSelectedRowsCommand = value
End Set
End Property
Public Sub New()
AddHandler Selection.CollectionChanged, AddressOf Selection_CollectionChanged
SelectOddRowsCommand = New DelegateCommand(Of Object)(AddressOf SelectOddRows)
DeleteSelectedRowsCommand = New DelegateCommand(Of Object)(AddressOf DeleteSelectedRows, AddressOf CanDeleteSelectedRows)
End Sub
Private Sub Selection_CollectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
DeleteSelectedRowsCommand.RaiseCanExecuteChanged()
End Sub
Private Sub SelectOddRows(ByVal obj As Object)
Selection.Clear()
For i As Integer = 0 To Source.Count - 1 Step 2
Selection.Add(Source(i))
Next i
End Sub
Private Sub DeleteSelectedRows(ByVal obj As Object)
Selection.ToList().ForEach(Function(item) Source.Remove(item))
End Sub
Private Function CanDeleteSelectedRows(ByVal obj As Object) As Boolean
Return Selection.Count > 0
End Function
End Class
Public Class DataItem
Public Shared Function GetData(Optional ByVal count As Integer = 100) As ObservableCollection(Of DataItem)
Dim result As New ObservableCollection(Of DataItem)()
For i As Integer = 0 To count - 1
result.Add(New DataItem(i, "Name" & i, Date.Now.AddDays(i)))
Next i
Return result
End Function
Public Property Id() As Integer
Public Property Name() As String
Public Property Date_Time() As Date
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal [date] As Date)
Me.Id = id
Me.Name = name
Date_Time = [date]
End Sub
Public Overrides Function ToString() As String
Return Name
End Function
End Class
End Namespace
The following code snippets (auto-collected from DevExpress Examples) contain references to the SelectedItems property.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
See Also