Working with TreeView in .NET MAUI TreeView (SfTreeView)

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)
{
   DisplayAlert("Message", "TreeView is Loaded", "Done");
}

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 provide the information for the 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.
<syncfusion:SfTreeView x:Name="treeView" ItemTapped="TreeView_ItemTapped" />
treeView.ItemTapped += TreeView_ItemTapped;

private void TreeView_ItemTapped(object sender, ItemTappedEventArgs e)
{
    DisplayAlert("Item Tapped", "TreeView item tapped", "Close");
}

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.
<syncfusion:SfTreeView x:Name="treeView" ItemDoubleTapped="TreeView_ItemDoubleTapped" />
treeView.ItemDoubleTapped += TreeView_ItemDoubleTapped;

private void TreeView_ItemDoubleTapped(object sender, ItemDoubleTappedEventArgs e)
{
    DisplayAlert("Item DoubleTapped", "TreeView item double tapped", "Close");
}

ItemRightTapped event

The ItemRightTapped event will be triggered whenever the item is right tapped. The ItemRightTappedEventArgs has the following members providing information for the ItemRightTapped event:

  • Node: Gets the TreeViewNode and data associated with the right-tapped item as its arguments.
  • Position: Gets the touch position in the right-tapped item.
<syncfusion:SfTreeView x:Name = "treeView" ItemRightTapped = "TreeView_ItemRightTapped" />
treeView.ItemRightTapped += TreeView_ItemRightTapped;

    private void TreeView_ItemRightTapped(object sender, ItemRightTappedEventArgs e)
    {
        DisplayAlert("Item RightTapped", "TreeView item right tapped", "Close");
    }

ItemLongPress event

The ItemLongPress event will be triggered whenever the item is long pressed.
ItemLongPressEventArgs has the following members which provides the information for the ItemLongPress 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.
<syncfusion:SfTreeView x:Name="treeView" ItemLongPress="TreeView_ItemLongPress" />
treeView.ItemLongPress += TreeView_ItemLongPress;
private void TreeView_ItemLongPress(object sender, ItemLongPressEventArgs e)
{
    DisplayAlert("Item LongPress", "TreeView item is Long Pressed","Close");   
}

KeyDown event

The KeyDown event is triggered when a key is pressed while the SfTreeView is in focus. The KeyPressEventArgs has the following members that provide information for the KeyDown event:

  • Key: Returns the currently pressed key.
  • IsShiftKeyPressed: Indicates whether the Shift key is in pressed state.
  • IsCtrlKeyPressed: Indicates whether the Control key is in pressed state.
  • IsAltKeyPressed: Indicates whether the Alt key is in pressed state.
  • IsCommandKeyPressed: Indicates whether the Command key is in pressed state.
  • Handled: Gets or sets whether the event is handled or not.
<syncfusion:SfTreeView x:Name="treeView" KeyDown="OnTreeViewKeyDown "/>
private void OnTreeViewKeyDown(object? sender, KeyPressEventArgs e)
{

}

Update the runtime changes

The PropertyChanged event will be triggered whenever a property in the customized 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 (e.PropertyName == "IsExpanded")
    {
        if (treeviewnode.IsExpanded)
            DisplayAlert("treeview", "nodeexpanded", "ok");
        else
            DisplayAlert("treeview", "nodecollapsed", "ok");
    }
}

Refresh layout

You can refresh the TreeViewNode from the root node and update all layout by using the SetDirty method that notifies the tree view layout mechanism to invalidate nodes.

node.SetDirty();