DataViewBase.SelectedRowsSource Property
Gets or sets the source of selected rows. This is a dependency property.
Namespace: DevExpress.Xpf.Grid
Assembly: DevExpress.Xpf.Grid.v19.1.Core.dll
Declaration
[Obsolete("Use the DataControlBase.SelectedItems property instead")]
public IList SelectedRowsSource { get; set; }
<Obsolete("Use the DataControlBase.SelectedItems property instead")>
Public Property SelectedRowsSource As IList
Property Value
Type |
Description |
IList |
The collection of selected rows.
|
Use the SelectedRowsSource property to synchronize rows selected within the grid with a ViewModel (MVVM). The source collection should implement INotifyCollectionChanged. Otherwise, changes made within the ViewModel are not reflected by the grid control.
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
See Also