|
1 | | -**[View document in Syncfusion Xamarin Knowledge base](https://www.syncfusion.com/kb/12128/how-to-add-or-remove-nodes-to-unbound-mode-treeview-in-xamarin-forms-sftreeview)** |
| 1 | +# add-or-remove-nodes-unbound-mode-treeview-xamarin |
| 2 | + |
| 3 | +This repository demonstrates how to add or remove nodes in unbound mode using the Xamarin.Forms SfTreeView control. It provides a sample implementation that shows how to programmatically manage the tree structure at runtime, enabling dynamic addition and removal of nodes without relying on data binding. |
| 4 | + |
| 5 | +## Sample |
| 6 | + |
| 7 | +### XAML |
| 8 | +```xaml |
| 9 | +<ContentPage.Behaviors> |
| 10 | + <behavior:Behavior/> |
| 11 | + </ContentPage.Behaviors> |
| 12 | + <ContentPage.Content> |
| 13 | + <StackLayout> |
| 14 | + <Grid HeightRequest="50" > |
| 15 | + <Button x:Name="addButton" Text="Add Node" /> |
| 16 | + <Button x:Name="deleteButton" Text="Delete Node" Grid.Column="1"/> |
| 17 | + </Grid> |
| 18 | + <syncfusion:SfTreeView x:Name="treeView" NotificationSubscriptionMode="CollectionChange"> |
| 19 | + <syncfusion:SfTreeView.Nodes> |
| 20 | + <treeviewengine:TreeViewNode x:Name="australia" Content="Australia" > |
| 21 | + <treeviewengine:TreeViewNode.ChildNodes> |
| 22 | + <treeviewengine:TreeViewNode Content="New South Wales"> |
| 23 | + <treeviewengine:TreeViewNode.ChildNodes> |
| 24 | + <treeviewengine:TreeViewNode Content="Sydney"/> |
| 25 | + <treeviewengine:TreeViewNode Content="Canberra"/> |
| 26 | + <treeviewengine:TreeViewNode Content="Newcastle–Maitland"/> |
| 27 | + </treeviewengine:TreeViewNode.ChildNodes> |
| 28 | + </treeviewengine:TreeViewNode> |
| 29 | + <treeviewengine:TreeViewNode Content="Victoria"> |
| 30 | + <treeviewengine:TreeViewNode.ChildNodes> |
| 31 | + <treeviewengine:TreeViewNode Content="Melbourne"/> |
| 32 | + </treeviewengine:TreeViewNode.ChildNodes> |
| 33 | + </treeviewengine:TreeViewNode> |
| 34 | + </treeviewengine:TreeViewNode.ChildNodes> |
| 35 | + </treeviewengine:TreeViewNode> |
| 36 | + </syncfusion:SfTreeView.Nodes> |
| 37 | + </syncfusion:SfTreeView> |
| 38 | + </StackLayout> |
| 39 | + </ContentPage.Content> |
| 40 | +</ContentPage> |
| 41 | +``` |
| 42 | + |
| 43 | +### Behavior |
| 44 | +```csharp |
| 45 | +public class Behavior : Behavior<ContentPage> |
| 46 | +{ |
| 47 | + #region Call back |
| 48 | + |
| 49 | + private void AddButton_Clicked(object sender, EventArgs e) |
| 50 | + { |
| 51 | + var india = new TreeViewNode() { Content = "India" }; |
| 52 | + var delhi = new TreeViewNode() { Content = "Delhi" }; |
| 53 | + var newDelhi = new TreeViewNode() { Content = "New Delhi" }; |
| 54 | + delhi.ChildNodes.Add(newDelhi); |
| 55 | + var tamilNadu = new TreeViewNode() { Content = "Tamil Nadu" }; |
| 56 | + var chennai = new TreeViewNode() { Content = "Chennai" }; |
| 57 | + india.ChildNodes.Add(delhi); |
| 58 | + india.ChildNodes.Add(tamilNadu); |
| 59 | + tamilNadu.ChildNodes.Add(chennai); |
| 60 | + |
| 61 | + TreeView.Nodes.Add(india); |
| 62 | + } |
| 63 | + |
| 64 | + private void DeleteButton_Clicked(object sender, EventArgs e) |
| 65 | + { |
| 66 | + TreeView.Nodes.RemoveAt(1); |
| 67 | + } |
| 68 | + |
| 69 | + #endregion |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Requirements to run the demo |
| 74 | +Visual Studio 2017 or Visual Studio for Mac. |
| 75 | +Xamarin add-ons for Visual Studio (available via the Visual Studio installer). |
| 76 | + |
| 77 | +## Troubleshooting |
| 78 | +### Path too long exception |
| 79 | +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. |
| 80 | + |
| 81 | +## License |
| 82 | + |
| 83 | +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