Selection in Xamarin TreeView (SfTreeView)
2 Oct 20236 minutes to read
This section explains how to perform selection and its related operations in the TreeView.
The TreeView allows selecting the items either programmatically or touch interactions by setting the SelectionMode property value to other than
None. The control has different selection modes to perform selection operations as listed as follows.
- None: Allows disabling the selection.
Single: Allows selecting the single item only. When clicking on the selected item, selection not will not be cleared. This is the default value for
- SingleDeselect: Allows selecting the single item only. When clicking on the selected item, selection gets cleared.
- Multiple: Allows selecting more than one item. Selection is not cleared when selecting more than one items. When clicking on the selected item, selection gets cleared.
Extended: Allows to select the multiple items using the common key modifiers in
UWP platformand for other platforms, it works as
<syncfusion:SfTreeView x:Name="TreeView" SelectionMode="Multiple"/>
TreeView.SelectionMode = SelectionMode.Multiple;
When the SelectionMode is other than
None, the item or items in the TreeView can be selected from the code by setting the SelectedItem, or adding items to the SelectedItems property based on the
When the selection mode is
SingleDeselect, programmatically select an item by setting the underlying object to the SelectedItem property.
treeView.SelectedItem = viewModel.CountriesInfo;
When the selection mode is
Multiple, programmatically select more than one item by adding the underlying object to the SelectedItems property.
If an item is selected programmatically when
Noneand if multiple items are programmatically selected when
SingleDeselect, then exception will be thrown internally.
Gets selected Items
The TreeView gets all the selected items through the
SelectedItems property and gets the single item by using the
Clear selected items
The selected items can be cleared by calling the
CurrentItem vs SelectedItem
The TreeView gets the selected item by using the SelectedItem and CurrentItem properties. Both
CurrentItem returns the same data object when selecting single item. When selecting more than one item, the
SelectedItem property returns the first selected item, and the
CurrentItem property returns the last selected item.
Select an entire row
By default, the selection starts from the indent level only. You can select the full row by enabling the FullRowSelect property. By setting the
true the selection spans the width of tree view control.
<syncfusion:SfTreeView x:Name="TreeView" FullRowSelect="True" />
TreeView.FullRowSelect = true;
Selected item style
The TreeView allows changing the selection background color for the selected items by using the SelectionBackgroundColor property. You can also change the selection background color at runtime.
The TreeView allows changing the selection foreground color for the selected items by using the SelectionForegroundColor property. You can also change the selection foreground color at runtime.
SelectionForegroundColoris applicable only for unbound mode.
The SelectionChanging event is raised while selecting an item at the execution time. The ItemSelectionChangingEventArgs has the following members which provides the information for
- AddedItems: Gets collection of the underlying data objects where the selection is going to process.
- RemovedItems: Gets collection of the underlying data objects where the selection is going to remove.
You can cancel the selection process within this event by setting the
ItemSelectionChangingEventArgs.Cancel property to true.
treeView.SelectionChanging += TreeView_SelectionChanging;
private void TreeView_SelectionChanging(object sender, ItemSelectionChangingEventArgs e)
if (e.AddedItems.Count > 0 && e.AddedItems == ViewModel.Items)
e.Cancel = true;
The SelectionChanged event will occur once selection process has been completed for the selected item in the TreeView. The ItemSelectionChangedEventArgs has the following members which provides information for
- AddedItems: Gets collection of the underlying data objects where the selection has been processed.
- RemovedItems: Gets collection of the underlying data objects where the selection has been removed.
treeView.SelectionChanged += TreeView_SelectionChanged;
private void TreeView_SelectionChanged(object sender, ItemSelectionChangedEventArgs e)
SelectionChangedevents will be triggered only on UI interactions.
The TreeView allows to select the items through keyboard interactions. Behavior of key navigation in UWP platform is explained as follows:
FocusBorderColorwill set to the
The FocusBorderColor property is used to set the border color for the current focused item. For Android and iOS platform, the default color is
Color.Transparent and for UWP platform, the default color is
Color.FromRgb(213, 213, 213).
The FocusBorderThickness property is used to set the border thickness for the current focused item. For Android and iOS platform, the default thickness is
0 and for UWP platform, the default thickness is
- When a grid is loaded inside the ItemTemplate with background color, the SelectionBackgroundColor will not display. Because, it overlaps the
SelectionBackgroundColor. In this case, set the background color for the TreeView instead of grid in the
- When the
TreeViewcontains duplicated items in the collection, only the first item whose instance was created initially will be selected or deselected.