The
RadListView
control is now obsolete and will be removed in the future. Use the RadCollectionView control instead. TheRadCollectionView
is a complete, ground-up rewrite of the ListView. TheRadCollectionView
offers improved performance, enhanced features, and a modernized approach to managing lists of data. TheRadCollectionView
incorporates all of the ListView's key features. More about the differences between both components and how to migrate to the newRadCollectionView
is available in the Migrating the Telerik .NET MAUI RadListView to RadCollectionView article.
.NET MAUI ListView Selection
The ListView component exposes selection feature. It allows single or multiple selection of the ListView items. This feature provides both visual and programmatic feedback for the actions of the user. Also you can disable the selection.
Selection Mode
The ListView provides three selection modes, which allow you to manipulate the type of selection. This is controlled by the SelectionMode
(Telerik.Maui.Controls.Compatibility.DataControls.ListView.SelectionMode
) property which has the following entries:
-
None
—This mode doesn't allow users to select an item. -
Single
—This is the default selection mode. It allows users to select only one item. -
Multiple
—This mode allows users to select more than one item.
Check below how you can set SelectionMode
in XAML and code-behind:
<telerik:RadListView x:Name="listView"
SelectionMode="Multiple" />
var listView = new RadListView();
listView.SelectionMode = Telerik.Maui.Controls.Compatibility.DataControls.ListView.SelectionMode.Multiple;
Selection Gestures
You can also configure how the selection to be triggered by the end users through the SelectionGesture
(Telerik.Maui.Controls.Compatibility.DataControls.ListView.SelectionGesture
) property:
-
Tap
—Tap on an item to select it. This is the defaultSelectionGesture
value. -
Hold
—Tap and hold an item to select it.
<telerikDataControls:RadListView x:Name="listView"
SelectionGesture="Hold" />
var listView = new RadListView();
listView.SelectionGesture = Telerik.Maui.Controls.Compatibility.DataControls.ListView.SelectionGesture.Hold;
Selected Item
The ListView provides the SelectedItem
(object
) property, which specifies the last selected item of the ListView.
Selected Items Collection
The ListView provides the SelectedItems
(ObservableCollection<object>
) property, which is a read-only collection used to get the currently selected items.
Selection Events
The ListView provides the SelectionChanged
event, which is triggered whenever the SelectedItems
collection is changed. The SelectionChanged
event handler receives two parameters:
- The sender argument which is of type object, but can be cast to the
RadListView
type. - A
NotifyCollectionChangedEventArgs
object which provides information on the collection changed event. For more details, refer to the NotifyCollectionChangedEventArgs Class topic.
Styling the Selected Item(s)
You can customize the way selected items look by applying SelectedItemStyle
property to the RadListView
instance. For detailed information on the approach, go to the Items Styles topic in ListView documentation.
Example
The example below shows how to use ListView selection feature and demonstrates how to set multiple selection, apply a selected item style, and retrieve the selected items in a ViewModel
class.
1. Create a ViewModel
class with two collections—One for the ItemsSource
of the ListView and one that will hold the SelectedItems
. For the example, the ListView is bound to a collection of strings:
public class ViewModel : NotifyPropertyChangedBase
{
private ObservableCollection<object> _selectedNames;
public ViewModel()
{
this.Names = new ObservableCollection<string>() { "Tom", "Anna", "Peter", "Teodor", "Lorenzo", "Andrea", "Martin" };
}
public ObservableCollection<string> Names { get; set; }
public ObservableCollection<object> SelectedNames
{
get { return this._selectedNames; }
set
{
if (this._selectedNames != value)
{
if (this._selectedNames != null)
{
this._selectedNames.CollectionChanged -= this.SelectedNamesCollectionChanged;
}
this._selectedNames = value;
if (this._selectedNames != null)
{
this._selectedNames.CollectionChanged += this.SelectedNamesCollectionChanged;
}
OnPropertyChanged("SelectedNames");
}
}
}
private void SelectedNamesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (this.SelectedNames.Count > 0)
{
App.DisplayAlert($"Selected items: {string.Join(",", this.SelectedNames.ToArray())}");
}
}
}
2. Add a RadListView
instance to your page with selection properties applied:
<telerik:RadListView x:Name="listView" Margin="10"
ItemsSource="{Binding Names}"
SelectionMode="Multiple"
SelectedItems="{Binding SelectedNames}">
<telerik:RadListView.SelectedItemStyle>
<telerik:ListViewItemStyle BackgroundColor="#88FFF5BA"
BorderColor="#88FFF5BA" />
</telerik:RadListView.SelectedItemStyle>
</telerik:RadListView>
3. Add the telerik
namespace:
xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
4. Set the ViewModel
class as a BindingContext
and call the InitializePickers()
method:
this.BindingContext = new ViewModel();
The following image shows how the ListView looks like on different platforms when multiple items are selected:
For the ListView Selection example, go to the SDKBrowser Demo Application and navigate to ListView -> Selection category.