diff --git a/README.md b/README.md index 4658bed..57554a6 100644 --- a/README.md +++ b/README.md @@ -1 +1,83 @@ -**[View document in Syncfusion Xamarin Knowledge base](https://www.syncfusion.com/kb/12160/how-to-highlight-nodes-when-searching-in-xamarin-forms-treeview-sftreeview)** +# highlight-nodes-on-search-treeview-xamarin + +You can highlight the TreeViewNode when searching in Xamarin.Forms SfTreeView. You can also refer to the search nodes in the SfTreeView document + +## Sample + +### XAML +Bind the model property to customise the BackgroundColor of the ItemTemplate. +```xaml + + + + + + + + + + + + + + + + + + +``` + +### Behavior +```csharp +public class Behavior : Behavior +{ + SearchBar SearchBar; + SfTreeView TreeView; + + protected override void OnAttachedTo(ContentPage bindable) + { + SearchBar = bindable.FindByName("searchBar"); + TreeView = bindable.FindByName("treeView"); + + SearchBar.TextChanged += SearchBar_TextChanged; + + base.OnAttachedTo(bindable); + } + + private void SearchBar_TextChanged(object sender, TextChangedEventArgs e) + { + this.TraverseNodes(TreeView.Nodes, e.NewTextValue); + } + + /// + /// Recursively traverse all the nodes in the TreeView and sets the BackgroundColor for the searched nodes. + /// + /// Represents the TreeViewNodes. + /// Represents the search key word. + 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()) ? Color.Teal : Color.Transparent; + + if (string.IsNullOrEmpty(searchText)) (child.Content as FileManager).NodeColor = Color.Transparent; + + if (child.ChildNodes != null) + { + this.TraverseNodes(child.ChildNodes, searchText); + } + } + } +} +``` + +## Requirements to run the demo + +To run the demo, refer to [System Requirements for Xamarin](https://help.syncfusion.com/xamarin/system-requirements) + +## Troubleshooting +### Path too long exception +If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project.