A newer version of this page is available. Switch to the current version.

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.1.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.

Remarks

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.

To learn more, see Obtaining Selected Rows and Cells.

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.

Examples

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>
See Also