How to do navigation on selection in Xamarin.Forms TreeView (SfTreeView)?
You can navigate to another page by selecting the TreeViewNode in Xamarin.Forms TreeView by binding SelectedItem property from ViewModel.
XAML
Binding ViewModel TreeViewSelectedItem to SfTreeView.SelectedItem.
ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TreeViewXamarin" xmlns:syncfusion="clr-namespace:Syncfusion.XForms.TreeView;assembly=Syncfusion.SfTreeView.XForms" x:Class="TreeViewXamarin.MainPage"> <ContentPage.BindingContext> <local:FileManagerViewModel x:Name="viewModel"/> </ContentPage.BindingContext> <ContentPage.Content> <syncfusion:SfTreeView x:Name="treeView" ChildPropertyName="SubFiles" SelectedItem="{Binding TreeViewSelectedItem, Mode=TwoWay}" ExpandActionTarget="Node" ItemTemplateContextType="Node" AutoExpandMode="None" 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> </ContentPage.Content> </ContentPage>
C#
Navigate to another page in the ViewModel TreeViewSelectedItem setter using PushAsync method.
namespace TreeViewXamarin { public class FileManagerViewModel { private FileManager treeViewSelectedItem; public FileManager TreeViewSelectedItem { get { return treeViewSelectedItem; } set { if (treeViewSelectedItem != value) { treeViewSelectedItem = value; ItemSelected(treeViewSelectedItem); } } } private async void ItemSelected(FileManager selectedItem) { await Task.Delay(250); var newPage = new NewPage(); newPage.BindingContext = selectedItem; await App.Current.MainPage.Navigation.PushAsync(newPage); } } }
Output
Conclusion
I hope you enjoyed learning about how to do navigation on selection in Xamarin.Forms TreeView (SfTreeView).
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!