Working with TreeView in Xamarin.iOS TreeView (SfTreeView)

27 Nov 20204 minutes to read

Interacting with TreeView Items

Loaded event

The Loaded event is raised when the TreeView is loading in view for the first time.

treeView.Loaded += TreeView_Loaded;

private void TreeView_Loaded(object sender, TreeViewLoadedEventArgs e)
{
	var alert = UIAlertController.Create ("TreeView", "Loaded in the view", UIAlertControllerStyle.Alert);
	alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
	PresentViewController (alert, true, null);
}

The Loaded event is used for the following use case:

Tapped event

The ItemTapped event will be triggered whenever tapping the item. ItemTappedEventArgs has the following members which provides the information for ItemTapped event:

  • Node: Gets the TreeViewNode and data associated with the tapped item as its arguments.
  • Position: Gets the touch position in the tapped item.
  • Handled: Gets or sets whether the event is handled or not.
treeView.ItemTapped += TreeView_ItemTapped;

private void TreeView_ItemTapped(object sender, ItemTappedEventArgs e)
{
	var alert = UIAlertController.Create ("TreeView", "ItemTapped", UIAlertControllerStyle.Alert);
	alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
	PresentViewController (alert, true, null);
}

ItemDoubleTapped event

The ItemDoubleTapped event will be triggered whenever double tapping the item. The ItemDoubleTappedEventArgs has the following members providing information for the ItemDoubleTapped event:

  • Node: Gets the TreeViewNode and data associated with the double tapped item as its arguments.
  • Position: Gets the touch position in the double tapped item.
  • Handled: Gets or sets whether the event is handled or not.
treeView.ItemDoubleTapped += TreeView_ItemDoubleTapped;

private void TreeView_ItemDoubleTapped(object sender, ItemDoubleTappedEventArgs e)
{
	var alert = UIAlertController.Create ("TreeView", "ItemDoubleTapped", UIAlertControllerStyle.Alert);
	alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
	PresentViewController (alert, true, null);
}

ItemHolding event

The ItemHolding event will be triggered whenever the item is long pressed.
ItemHoldingEventArgs has the following members which provides the information for ItemHolding event:

  • Node: Gets the TreeViewNode and data associated with the hold item as its arguments.
  • Position: Gets the touch position in the hold item.
  • Handled: Gets or sets whether the event is handled or not.
treeView.ItemHolding += TreeView_ItemHolding;
private void TreeView_ItemHolding(object sender, ItemHoldingEventArgs e)
{
	var alert = UIAlertController.Create ("TreeView", "ItemHolding", UIAlertControllerStyle.Alert);
	alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
	PresentViewController (alert, true, null);
}

Update the runtime changes

The PropertyChanged event will be triggered whenever a properties in TreeViewNode is changed. You can get the name of the property that changed by using the PropertyName property of the PropertyChangedEventArgs.

treeviewnode.PropertyChanged += Treeviewnode_PropertyChanged;

private void Treeviewnode_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (treeviewnode.IsExpanded)
        DisplayAlert("treeview", "nodeexpanded", "ok");
    else
        DisplayAlert("treeview", "nodecollapsed", "ok");
}

Refresh layout

SetDirty notifies the TreeViewNode to recalculate the child collection update mechanism to invalidate that node which helps to update the engine and refresh the UI.

node.SetDirty();