|
1 | | -**[View document in Syncfusion Xamarin Knowledge base](https://www.syncfusion.com/kb/12488/how-to-do-conditional-navigation-in-xamarin-forms-treeview-sftreeview)** |
| 1 | +# conditional-navigation-treeview-xamarin |
| 2 | +This repository demonstrates how to implement conditional navigation in a Syncfusion Xamarin.Forms TreeView. The sample shows how to navigate to different pages or perform specific actions based on which node the user selects in the TreeView. By applying conditions in the selection changed event or command, you can control navigation flow, restrict access, or trigger custom logic dynamically according to your application's requirements. |
| 3 | + |
| 4 | +## Sample |
| 5 | + |
| 6 | +### XAML |
| 7 | + |
| 8 | +Bind the SfTreeView.TapCommand to navigate to another page. By default, the TreeViewNode is the command parameter for the TapCommand. |
| 9 | + |
| 10 | +```xaml |
| 11 | +<syncfusion:SfTreeView x:Name="treeView" |
| 12 | + ChildPropertyName="SubFiles" |
| 13 | + ItemTemplateContextType="Node" |
| 14 | + AutoExpandMode="AllNodesExpanded" |
| 15 | + ItemsSource="{Binding ImageNodeInfo}" |
| 16 | + TapCommand="{Binding TreeViewTapCommand}"> |
| 17 | + <syncfusion:SfTreeView.ItemTemplate> |
| 18 | + <DataTemplate> |
| 19 | + <Grid x:Name="grid" RowSpacing="0" > |
| 20 | + ... |
| 21 | + </Grid> |
| 22 | + </DataTemplate> |
| 23 | + </syncfusion:SfTreeView.ItemTemplate> |
| 24 | +</syncfusion:SfTreeView> |
| 25 | +``` |
| 26 | + |
| 27 | +### View Model |
| 28 | + |
| 29 | +In the command execution method, skip the navigation for nodes based on TreeViewNode.Level. |
| 30 | + |
| 31 | +```csharp |
| 32 | +public class FileManagerViewModel |
| 33 | +{ |
| 34 | + public FileManagerViewModel() |
| 35 | + { |
| 36 | + TreeViewTapCommand = new Command<object>(OnTapped); |
| 37 | + } |
| 38 | + |
| 39 | + public Command<object> TreeViewTapCommand { get; set; } |
| 40 | + |
| 41 | + private void OnTapped(object obj) |
| 42 | + { |
| 43 | + var node = obj as Syncfusion.TreeView.Engine.TreeViewNode; |
| 44 | + |
| 45 | + if (node.Level == 0) return; |
| 46 | + |
| 47 | + var newPage = new Views.NewPage(); |
| 48 | + newPage.BindingContext = node; |
| 49 | + App.Current.MainPage.Navigation.PushAsync(newPage); |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Requirements to run the demo |
| 55 | +Visual Studio 2017 or Visual Studio for Mac. |
| 56 | +Xamarin add-ons for Visual Studio (available via the Visual Studio installer). |
| 57 | + |
| 58 | +## Troubleshooting |
| 59 | +### Path too long exception |
| 60 | +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. |
| 61 | + |
| 62 | +## License |
| 63 | + |
| 64 | +Syncfusion® has no liability for any damage or consequence that may arise from using or viewing the samples. The samples are for demonstrative purposes. If you choose to use or access the samples, you agree to not hold Syncfusion® liable, in any form, for any damage related to use, for accessing, or viewing the samples. By accessing, viewing, or seeing the samples, you acknowledge and agree Syncfusion®'s samples will not allow you seek injunctive relief in any form for any claim related to the sample. If you do not agree to this, do not view, access, utilize, or otherwise do anything with Syncfusion®'s samples. |
0 commit comments