Class SfTreeView
A SfTreeView class provides a way to display the information in hierarchical structure.
Inheritance
Implements
Namespace: Syncfusion.Android.TreeView
Assembly: Syncfusion.SfTreeView.Android.dll
Syntax
public class SfTreeView : FrameLayout, IDisposable, ITreeCollectionView
Examples
This example demonstrates how to initialize SfTreeView in XAML.
<syncfusion:SfTreeView x:Name="treeView"
ChildPropertyName="Files"
ItemsSource="{Binding Folders}">
<syncfusion:SfTreeView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation = "Horizontal">
<ContentPresenter Width="20"
Height="20"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
ContentTemplate="{Binding ImageTemplate}"/>
<TextBlock Margin = "5"
VerticalAlignment="Center"
Text="{Binding FileName}"/>
</StackPanel>
</DataTemplate>
</syncfusion:SfTreeView.ItemTemplate>
</syncfusion:SfTreeView>
public class NodeWithImageViewModel
{
public ObservableCollection<Folder> Folders { get; internal set; }
private ResourceDictionary CommonResourceDictionary { get; set; }
public NodeWithImageViewModel()
{
CommonResourceDictionary = new ResourceDictionary() { Source = new Uri("ms-appx:///Icons/PathIcon.xaml", UriKind.RelativeOrAbsolute) };
this.Folders = GetFiles();
}
private ObservableCollection<Folder> GetFiles()
{
var nodeImageInfo = new ObservableCollection<Folder>();
var doc = new Folder() { FileName = "Documents", ImageTemplate = CommonResourceDictionary["Folder"] as DataTemplate };
var download = new Folder() { FileName = "Downloads", ImageTemplate = CommonResourceDictionary["Folder"] as DataTemplate };
var pollution = new Folder() { FileName = "Environmental Pollution.docx", ImageTemplate = CommonResourceDictionary["Word"] as DataTemplate };
var globalWarming = new Folder() { FileName = "Global Warming.ppt", ImageTemplate = CommonResourceDictionary["PowerPoint"] as DataTemplate };
var sanitation = new Folder() { FileName = "Sanitation.docx", ImageTemplate = CommonResourceDictionary["Word"] as DataTemplate };
var games = new Folder() { FileName = "Game.exe", ImageTemplate = CommonResourceDictionary["EXE"] as DataTemplate };
var tutorials = new Folder() { FileName = "Tutorials.zip", ImageTemplate = CommonResourceDictionary["Zip"] as DataTemplate };
doc.Files = new ObservableCollection<Folder>
{
pollution,
globalWarming,
sanitation
};
download.Files = new ObservableCollection<Folder>
{
games,
tutorials
};
nodeImageInfo.Add(doc);
nodeImageInfo.Add(download);
return nodeImageInfo;
}
}
public class Folder : NotificationObject
{
private string fileName;
private DataTemplate imageTemplate;
private ObservableCollection<Folder> files;
public Folder()
{
}
public ObservableCollection<Folder> Files
{
get { return files; }
internal set
{
files = value;
RaisePropertyChanged(nameof(Files));
}
}
public string FileName
{
get { return fileName; }
set
{
fileName = value;
RaisePropertyChanged(nameof(FileName));
}
}
public DataTemplate ImageTemplate
{
get { return imageTemplate; }
set { imageTemplate = value; }
}
}
Constructors
SfTreeView(Context)
Initializes a new instance of the SfTreeView class.
Declaration
public SfTreeView(Context context)
Parameters
Type | Name | Description |
---|---|---|
Android.Content.Context | context | Represent the Android.Content.Context of SfTreeView. |
SfTreeView(Context, IAttributeSet)
Initializes a new instance of the SfTreeView class.
Declaration
public SfTreeView(Context context, IAttributeSet attribute)
Parameters
Type | Name | Description |
---|---|---|
Android.Content.Context | context | Represent the Android.Content.Context of SfTreeView. |
Android.Util.IAttributeSet | attribute | Represent the Android.Util.IAttributeSet of SfTreeView. |
SfTreeView(Context, IAttributeSet, Int32)
Initializes a new instance of the SfTreeView class.
Declaration
public SfTreeView(Context context, IAttributeSet attribute, int defaultStyleAttribute)
Parameters
Type | Name | Description |
---|---|---|
Android.Content.Context | context | Represent the Android.Content.Context of SfTreeView. |
Android.Util.IAttributeSet | attribute | Represent the Android.Util.IAttributeSet of SfTreeView. |
System.Int32 | defaultStyleAttribute | The default style |
SfTreeView(Context, IAttributeSet, Int32, Int32)
Initializes a new instance of the SfTreeView class.
Declaration
public SfTreeView(Context context, IAttributeSet attribute, int defaultStyleAttribute, int defaultStyleResource)
Parameters
Type | Name | Description |
---|---|---|
Android.Content.Context | context | Represent the Android.Content.Context of SfTreeView. |
Android.Util.IAttributeSet | attribute | Represent the Android.Util.IAttributeSet of SfTreeView. |
System.Int32 | defaultStyleAttribute | The default style |
System.Int32 | defaultStyleResource | The default resource |
Properties
Adapter
Gets or sets the TreeViewAdapter to customize the each TreeViewItem's.
Declaration
public TreeViewAdapter Adapter { get; set; }
Property Value
Type |
---|
TreeViewAdapter |
AutoExpandMode
Gets or sets the value which indicates how the nodes to be expanded while loading.
Declaration
public AutoExpandMode AutoExpandMode { get; set; }
Property Value
Type | Description |
---|---|
AutoExpandMode | The default value is None. |
ChildPropertyName
Gets or sets the property name for the child object where ItemsSource is used to define the items for this tree.
Declaration
public string ChildPropertyName { get; set; }
Property Value
Type |
---|
System.String |
CurrentItem
Gets or sets the current selected item.
Declaration
public object CurrentItem { get; set; }
Property Value
Type | Description |
---|---|
System.Object | The current item. |
ExpandActionTarget
Gets or sets a value indicating whether expanding and collapsing of nodes can be performed only by tapping in expander view or in both expander view and content view.
Declaration
public ExpandActionTarget ExpandActionTarget { get; set; }
Property Value
Type |
---|
ExpandActionTarget |
ExpanderPosition
Gets or sets the expander position in SfTreeView. Default value is Start.
Declaration
public ExpanderPosition ExpanderPosition { get; set; }
Property Value
Type |
---|
ExpanderPosition |
ExpanderWidth
Gets or sets the width of expander in TreeViewItem.
Declaration
public double ExpanderWidth { get; set; }
Property Value
Type |
---|
System.Double |
FullRowSelect
Gets or sets a value indicating whether the selection spans the width of tree view control.
Declaration
public bool FullRowSelect { get; set; }
Property Value
Type |
---|
System.Boolean |
Remarks
Default value false, where the selection start from indent level only.
HierarchyPropertyDescriptors
Gets or sets the list of HierarchyPropertyDescriptors which holds list of HierarchyPropertyDescriptor that provides information of type and property name. TreeView generates the child nodes based ChildPropertyName if the type of data object matches TargetType.
Declaration
public HierarchyPropertyDescriptors HierarchyPropertyDescriptors { get; set; }
Property Value
Type |
---|
HierarchyPropertyDescriptors |
Indentation
Gets or sets the distance to indent each child TreeViewItem.
Declaration
public double Indentation { get; set; }
Property Value
Type |
---|
System.Double |
IsAnimationEnabled
Gets or sets a value indicating whether to enable animation while expanding and collapse items.
Declaration
public bool IsAnimationEnabled { get; set; }
Property Value
Type |
---|
System.Boolean |
IsScrollBarVisible
Gets or sets a value indicating whether to display the scrollbar or not.
Declaration
public bool IsScrollBarVisible { get; set; }
Property Value
Type |
---|
System.Boolean |
Remarks
ScrollBar cannot be updated at runtime.
Examples
SfTreeView treeView = new SfTreeView();
treeView.IsScrollBarVisible = true;
ItemHeight
Gets or sets the height of each TreeViewItem in SfTreeView.
Declaration
public double ItemHeight { get; set; }
Property Value
Type |
---|
System.Double |
ItemsSource
Gets or sets the source items to the template and display in SfTreeView.
Declaration
public object ItemsSource { get; set; }
Property Value
Type |
---|
System.Object |
NodePopulationMode
Gets or sets the value that indicates whether the child nodes should be populated when SfTreeView is loaded or when the node is expanded only.
Declaration
public TreeNodePopulationMode NodePopulationMode { get; set; }
Property Value
Type |
---|
TreeNodePopulationMode |
Remarks
When NodePopulationMode is OnDemand, the child nodes will be populated when the parent nodes are expanded only. When NodePopulationMode is Instant, all the nodes will be populated when SfTreeView initially loaded. The default value is OnDemand. This property is applicable for Bound mode only.
Nodes
Gets or sets the collection of tree view nodes that are assigned to tree view control in Unbound mode..
Declaration
public TreeViewNodeCollection Nodes { get; set; }
Property Value
Type |
---|
TreeViewNodeCollection |
NotificationSubscriptionMode
Gets or sets the value that indicates whether to subscribe collection and property changes of data object to perform real time data update. When binding ItemsSource, TreeView subscribes to collection and property changes of data object based on NotificationSubscriptionMode.
Declaration
public NotificationSubscriptionMode NotificationSubscriptionMode { get; set; }
Property Value
Type |
---|
NotificationSubscriptionMode |
Remarks
When NotificationSubscriptionMode is CollectionChange, TreeView updated its tree structure when child items collection gets changed. When NotificationSubscriptionMode is Property change, TreeView updates its ChildItems when associated collection property gets changed. The default value is None.
Examples
SfTreeView treeView = new SfTreeView();
treeView.NotificationSubscriptionMode = NotificationSubscriptionMode.CollectionChange | NotificationSubscriptionMode.PropertyChange;
SelectedItem
Gets or sets the first selected item.
Declaration
public object SelectedItem { get; set; }
Property Value
Type | Description |
---|---|
System.Object | The selected item. |
SelectedItems
Gets or sets the selected items for selection.
Declaration
public ObservableCollection<object> SelectedItems { get; set; }
Property Value
Type |
---|
System.Collections.ObjectModel.ObservableCollection<System.Object> |
SelectionBackgroundColor
Gets or sets the selection background color for the selected item.
Declaration
public Color SelectionBackgroundColor { get; set; }
Property Value
Type |
---|
Android.Graphics.Color |
SelectionController
Gets or sets the SelectionController to process and update the selections for TreeViewItem.
Declaration
public SelectionController SelectionController { get; set; }
Property Value
Type |
---|
SelectionController |
SelectionForegroundColor
Gets or sets the selected item foreground color. Note: It will applicable for unbound mode only.
Declaration
public Color SelectionForegroundColor { get; set; }
Property Value
Type |
---|
Android.Graphics.Color |
SelectionMode
Gets or sets the selection mode for selection.
Declaration
public SelectionMode SelectionMode { get; set; }
Property Value
Type |
---|
SelectionMode |
Remarks
By default value, the SelectionMode is SelectionMode.Single.
Methods
BringIntoView(TreeViewNode, Boolean, Boolean, ScrollToPosition)
Attempts to bring the tree view item into view by scrolling.
Declaration
public void BringIntoView(TreeViewNode item, bool disableAnimation = false, bool canExpand = false, ScrollToPosition scrollToPosition)
Parameters
Type | Name | Description |
---|---|---|
TreeViewNode | item | Represents the TreeViewNode to scroll. |
System.Boolean | disableAnimation | true to disable scroll animation while changing the view; otherwise, false. The default is false. |
System.Boolean | canExpand | true to expand the collapsed nodes and bring the collapsed node into view; otherwise, false. The default is false. |
ScrollToPosition | scrollToPosition | Defines position in the view to which node is scrolled. |
Remarks
Need to set NodePopulationMode property to Instant for populating all the nodes to bring the collapsed node into view while calling this method with 'canExpand' parameter as true.
BringIntoView(Object, Boolean, Boolean, ScrollToPosition)
Attempts to bring the TreeViewItem into view by scrolling.
Declaration
public void BringIntoView(object dataObject, bool disableAnimation = false, bool canExpand = false, ScrollToPosition scrollToPosition)
Parameters
Type | Name | Description |
---|---|---|
System.Object | dataObject | Represents the data object associated with TreeViewNode to scroll. |
System.Boolean | disableAnimation | true to disable scroll animation while changing the view; otherwise, false. The default is false. |
System.Boolean | canExpand | true to expand the collapsed nodes and bring the collapsed node into view; otherwise, false. The default is false. |
ScrollToPosition | scrollToPosition | Defines position in the view to which object is scrolled. |
Remarks
Need to set NodePopulationMode property to Instant for populating all the nodes to bring the collapsed node into view while calling this method with 'canExpand' parameter as true.
CollapseAll()
Collapses all the nodes in the SfTreeView.
Declaration
public void CollapseAll()
CollapseNode(TreeViewNode)
Collapses the specified TreeViewNode.
Declaration
public void CollapseNode(TreeViewNode treeViewNode)
Parameters
Type | Name | Description |
---|---|---|
TreeViewNode | treeViewNode | Specifies the TreeViewNode to collapse it from the view. |
CollapseNodes(Int32)
Collapses all the tree view items based on the specified level.
Declaration
public void CollapseNodes(int level = -1)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | level | Specifies the level of TreeViewNode to collapse all the TreeViewItems from the view. |
Dispose(Boolean)
Dispose the instances, if parameter is true.
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | disposing | Represents the boolean value for disposing objects. |
ExpandAll()
Expands all the nodes in the SfTreeView.
Declaration
public void ExpandAll()
ExpandNode(TreeViewNode)
Expands the specified TreeViewNode.
Declaration
public void ExpandNode(TreeViewNode treeViewNode)
Parameters
Type | Name | Description |
---|---|---|
TreeViewNode | treeViewNode | Specifies the TreeViewNode to expand it in the view. |
ExpandNodes(Int32)
Expands all the tree view items based on the specified level.
Declaration
public void ExpandNodes(int level = -1)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | level | Specifies the level of TreeViewNode to expand all the TreeViewItems in the view. |
OnAttachedToWindow()
Raises when the SfTreeView is attached into a window.
Declaration
protected override void OnAttachedToWindow()
OnDetachedFromWindow()
Raises when the SfTreeView is detached from a window.
Declaration
protected override void OnDetachedFromWindow()
OnLayout(Boolean, Int32, Int32, Int32, Int32)
Positions and sizes the children of the SfTreeView.
Declaration
protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | changed | Indicates whether the layout has changed or not. |
System.Int32 | left | The current extreme left position of the SfTreeView. |
System.Int32 | top | The current top position of the SfTreeView. |
System.Int32 | right | The current right extreme position of the SfTreeView. |
System.Int32 | bottom | The current bottom position of the SfTreeView. |
OnMeasure(Int32, Int32)
This method is used to get the desired size of an element.
Declaration
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | widthMeasureSpec | The available width for the element to use. |
System.Int32 | heightMeasureSpec | The available height for the element to use. |
RefreshView()
Refresh the TreeViewItem in UI.
Declaration
public void RefreshView()
ResetTreeViewItems(Object)
Method to reset the visible TreeViewItem’s. If parameter is null, visible tree view items will reset else, particular TreeViewItem will reset.
Declaration
public void ResetTreeViewItems(object data = null)
Parameters
Type | Name | Description |
---|---|---|
System.Object | data | For Unbound mode, data represents the TreeViewNode and for Bound mode, it represents the data object associated with TreeViewNode. |
SelectAll()
Select all the items in the SfTreeView.
Declaration
public void SelectAll()
Events
ItemDoubleTapped
Occurs when the TreeViewItem's ContentView is double tapped.
Declaration
public event EventHandler<ItemDoubleTappedEventArgs> ItemDoubleTapped
Event Type
Type |
---|
System.EventHandler<ItemDoubleTappedEventArgs> |
ItemHolding
Occurs when the TreeViewItem's ContentView is long pressed.
Declaration
public event EventHandler<ItemHoldingEventArgs> ItemHolding
Event Type
Type |
---|
System.EventHandler<ItemHoldingEventArgs> |
ItemTapped
Occurs when the TreeViewItem's ContentView is tapped.
Declaration
public event EventHandler<ItemTappedEventArgs> ItemTapped
Event Type
Type |
---|
System.EventHandler<ItemTappedEventArgs> |
Loaded
Occurs when the SfTreeView is loaded for the first time.
Declaration
public event EventHandler<TreeViewLoadedEventArgs> Loaded
Event Type
Type |
---|
System.EventHandler<TreeViewLoadedEventArgs> |
LoadOnDemand
Occurs when the TreeViewItem is loaded at run time.
Declaration
public event EventHandler<LoadOnDemandEventArgs> LoadOnDemand
Event Type
Type |
---|
System.EventHandler<LoadOnDemandEventArgs> |
Examples
SfTreeView treeView = new SfTreeView();
treeView.LoadOnDemand += TreeView_LoadOnDemandEvent;
private async void TreeView_LoadOnDemandEvent(object sender, LoadOnDemandEventArgs e)
{
if (e.Action == LoadOnDemandAction.RequestStatus)
{
...
if (!hasChildNodes)
{
e.HasChildNodes = false;
return;
}
e.HasChildNodes = true;
}
else if(e.Action == Action.PopulateNodes)
{
...
node.ShowExpanderAnimation = true;
...
node.PopulateChildNodes(items);
if (items.Count() > 0)
node.IsExpanded = true;
node.ShowExpanderAnimation = false;
}
}
NodeCollapsed
Occurs when an TreeViewNode is collapsed.
Declaration
public event EventHandler<NodeExpandedCollapsedEventArgs> NodeCollapsed
Event Type
Type |
---|
System.EventHandler<NodeExpandedCollapsedEventArgs> |
NodeCollapsing
Occurs when an TreeViewNode is being collapsed.
Declaration
public event EventHandler<NodeExpandingCollapsingEventArgs> NodeCollapsing
Event Type
Type |
---|
System.EventHandler<NodeExpandingCollapsingEventArgs> |
NodeExpanded
Occurs when an TreeViewNode is expanded.
Declaration
public event EventHandler<NodeExpandedCollapsedEventArgs> NodeExpanded
Event Type
Type |
---|
System.EventHandler<NodeExpandedCollapsedEventArgs> |
NodeExpanding
Occurs when an TreeViewNode is being expanded.
Declaration
public event EventHandler<NodeExpandingCollapsingEventArgs> NodeExpanding
Event Type
Type |
---|
System.EventHandler<NodeExpandingCollapsingEventArgs> |
QueryNodeSize
Occurs whenever an item comes into view and query the item size.
Declaration
public event EventHandler<QueryNodeSizeEventArgs> QueryNodeSize
Event Type
Type |
---|
System.EventHandler<QueryNodeSizeEventArgs> |
Remarks
SfTreeView supports auto height feature, by using this event. We can define whether the specified size can be set to the item or not by handling the QueryNodeSize event.
Examples
This example demonstrates how to handle QueryNodeSize event.
using Syncfusion.Android.TreeView;
SfTreeView treeView = new SfTreeView();
treeView.QueryNodeSize += TreeView_QueryNodeSize;
void TreeView_QueryNodeSize (object sender, QueryNodeSizeEventArgs e)
{
e.Height = 40;
e.Handled = true;
}
SelectionChanged
Occurs once the selection process has been completed for the selected item in the SfTreeView.
Declaration
public event EventHandler<ItemSelectionChangedEventArgs> SelectionChanged
Event Type
Type |
---|
System.EventHandler<ItemSelectionChangedEventArgs> |
SelectionChanging
Occurs while selecting an item's contentview at the execution time.
Declaration
public event EventHandler<ItemSelectionChangingEventArgs> SelectionChanging
Event Type
Type |
---|
System.EventHandler<ItemSelectionChangingEventArgs> |
Remarks
The SelectionChanging event is a cancelable event and can be canceled by handling the event and setting the Cancel property to true. However, if the event is canceled, the selection process for the selected TreeViewItem's contentview gets cancelled.