Articles in this section

How to highlight nodes in .NET MAUI TreeView (SfTreeView)?

You can highlight a TreeViewNode that match the search criteria in .NET MAUI TreeView by configuring the background property within the ItemTemplate.

Bind the model property to BackgroundColor property of Grid inside the ItemTemplate.

<Grid RowDefinitions="Auto,*">
    <SearchBar x:Name="searchBar" 
               Placeholder="Search TreeView"/>
    <syncfusion:SfTreeView x:Name="treeView"
                           Grid.Row="1" 
                           ChildPropertyName="SubFiles" 
                           ItemTemplateContextType="Node"
                           ItemHeight="25"
                           AutoExpandMode="AllNodesExpanded" 
                           ItemsSource="{Binding ImageNodeInfo}">
        <syncfusion:SfTreeView.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="grid" 
                      RowSpacing="0" 
                      BackgroundColor="{Binding Content.NodeColor}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Image Source="{Binding Content.ImageIcon}" 
                           VerticalOptions="Center" 
                           HorizontalOptions="Center" 
                           HeightRequest="20" 
                           WidthRequest="20"/>
                    <Grid Grid.Column="1" 
                          RowSpacing="1" 
                          Padding="1,0,0,0" 
                          VerticalOptions="Center">
                        <Label LineBreakMode="NoWrap" 
                               FontSize="12" 
                               Text="{Binding Content.ItemName}" 
                               VerticalTextAlignment="Center"/>
                    </Grid>
                </Grid>
            </DataTemplate>
        </syncfusion:SfTreeView.ItemTemplate>
    </syncfusion:SfTreeView>
</Grid>

Set the BackgroundColor for the filtered items.

public class ContentPageBehavior : Behavior<ContentPage>
{
    SearchBar SearchBar;
    SfTreeView TreeView;
    
    protected override void OnAttachedTo(ContentPage bindable)
    {
        SearchBar = bindable.FindByName<SearchBar>("searchBar");
        TreeView = bindable.FindByName<SfTreeView>("treeView");
        SearchBar.TextChanged += SearchBar_TextChanged;
        base.OnAttachedTo(bindable);
    }

    private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
    {
        this.TraverseNodes(TreeView.Nodes, e.NewTextValue);
    }

    private void TraverseNodes(TreeViewNodeCollection nodes, string searchText)
    {
        foreach (var child in nodes)
        {
            (child.Content as FileManager).NodeColor = (child.Content as FileManager).ItemName.ToLower().StartsWith(searchText.ToLower()) ? Colors.Teal : Colors.Transparent;

            if (string.IsNullOrEmpty(searchText)) (child.Content as FileManager).NodeColor = Colors.Transparent;

            if (child.ChildNodes != null)
            {
                this.TraverseNodes(child.ChildNodes, searchText);
            }
        }
    }
}

Output

highlight-node-when-search-in-treeview.gif

Download the complete sample from GitHub

Conclusion

I hope you enjoyed learning about how to highlight nodes in .NET MAUI TreeView.

You can refer to our .NET MAUI TreeView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications.

You can also explore our .NET MAUI TreeView example to understand how to create and manipulate data.

You can check out our components from the License and Downloads page for current customers. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.

If you have any queries or require clarification, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied