Managing SelectedItems in TreeDataGrid
Environment
| Version | Product | Author |
|---|---|---|
| 12.1.0 | Telerik UI for .NET MAUI TreeDataGrid | Dobrinka Yordanova |
Description
I want to manage the SelectedItems property in the Telerik TreeDataGrid for .NET MAUI. The SelectedItems collection is read-only, inaccessible in XAML, and cannot use two-way binding. This is by design and applies to similar collections in Telerik MAUI controls like AutoComplete Tokens, ComboBox SelectedItems, DataGrid SelectedItems, and CollectionView SelectedItems.
This knowledge base article also answers the following questions:
- How to bind the
SelectedItemsproperty in TreeDataGrid? - How to manage the
SelectedItemscollection in Telerik MAUI controls? - How to handle
CollectionChangedevents?
Solution
To manage the SelectedItems collection, bind it to a property in the ViewModel and handle property changes using the CollectionChanged event.
1. Define the ViewModel with an ObservableCollection for SelectedItems. Subscribe to the CollectionChanged event.
public class ViewModel : NotifyPropertyChangedBase
{
private ObservableCollection<object> selectedItems;
public ObservableCollection<Data> Items { get; set; }
public ObservableCollection<object> SelectedItems
{
get => this.selectedItems;
set
{
if (this.selectedItems != value)
{
if (this.selectedItems != null)
{
this.selectedItems.CollectionChanged -= SelectedItems_CollectionChanged;
}
this.selectedItems = value;
if (this.selectedItems != null)
{
this.selectedItems.CollectionChanged += SelectedItems_CollectionChanged;
}
OnPropertyChanged();
}
}
}
private void SelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
// Handle added items
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
// Handle removed items
}
}
}
2. Bind the TreeDataGrid's SelectedItems and ItemsSource properties to the ViewModel.
<telerik:RadTreeDataGrid x:Name="treeDataGrid"
SelectionMode="Multiple"
SelectedItems="{Binding SelectedItems}"
ItemsSource="{Binding Items}"/>