How to bring the TreeView child node to view when the root node is collapsed in Xamarin.Forms?
You can programmatically bring the child node to the view when all root nodes are collapsed using the BringIntoView method by expanding the specific root node in Xamarin.Forms TreeView.
XAML
Use the SfTreeView.NodePopulationMode property as Instant for SfTreeView.
<syncfusion:SfTreeView x:Name="treeView" ChildPropertyName="SubFiles" NodePopulationMode="Instant" ItemTemplateContextType="Node" ItemsSource="{Binding ImageNodeInfo}"> <syncfusion:SfTreeView.ItemTemplate> <DataTemplate> <Grid x:Name="grid" RowSpacing="0" > <Grid.ColumnDefinitions> <ColumnDefinition Width="40" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Source="{Binding Content.ImageIcon}" VerticalOptions="Center" HorizontalOptions="Center" HeightRequest="35" WidthRequest="35"/> <Grid Grid.Column="1" RowSpacing="1" Padding="1,0,0,0" VerticalOptions="Center"> <Label LineBreakMode="NoWrap" Text="{Binding Content.ItemName}" VerticalTextAlignment="Center"/> </Grid> </Grid> </DataTemplate> </syncfusion:SfTreeView.ItemTemplate> </syncfusion:SfTreeView>
C#
In the Button.Clicked event, bring any child item using the BringIntoView method.
public class Behavior : Behavior<ContentPage> { SfTreeView TreeView; Button Button; protected override void OnAttachedTo(ContentPage bindable) { TreeView = bindable.FindByName<SfTreeView>("treeView"); Button = bindable.FindByName<Button>("button"); Button.Clicked += Button_Clicked; base.OnAttachedTo(bindable); } private void Button_Clicked(object sender, EventArgs e) { var viewModel = (sender as Button).BindingContext as FileManagerViewModel; var item = viewModel.ImageNodeInfo[3].SubFiles[1]; TreeView.BringIntoView(item, true, true); TreeView.SelectedItem = item; } }
Output
Conclusion
I hope you enjoyed learning about how to bring the TreeView child node to view when the root is collapsed in Xamarin.Forms TreeView.
You can refer to our Xamarin.Forms TreeView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, 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!