|
1 | | -**[View document in Syncfusion Xamarin Knowledge base](https://www.syncfusion.com/kb/12246/how-to-customize-the-item-template-in-an-unbound-xamarin-forms-treeview-sftreeview)** |
| 1 | +# customize-the-itemtemplate-unbound-treeview-xamarin |
| 2 | + |
| 3 | +This repository demonstrates how to customize the item template in an unbound Xamarin.Forms TreeView (SfTreeView) control. It provides a sample implementation that shows how to define and style TreeView nodes directly in XAML without data binding, enabling flexible and visually distinct hierarchical layouts. |
| 4 | + |
| 5 | +## Sample |
| 6 | + |
| 7 | +### XAML |
| 8 | +```xaml |
| 9 | +<ContentPage.Resources> |
| 10 | + <ResourceDictionary> |
| 11 | + <helper:TextColorConverter x:Key="textColorConverter"/> |
| 12 | + </ResourceDictionary> |
| 13 | + </ContentPage.Resources> |
| 14 | + |
| 15 | + <StackLayout> |
| 16 | + <syncfusion:SfTreeView x:Name="treeView"> |
| 17 | + <syncfusion:SfTreeView.ItemTemplate> |
| 18 | + <DataTemplate> |
| 19 | + <Grid> |
| 20 | + <Label Text="{Binding Content}" TextColor="{Binding Level, Converter={StaticResource textColorConverter}}" VerticalOptions="Center"/> |
| 21 | + </Grid> |
| 22 | + </DataTemplate> |
| 23 | + </syncfusion:SfTreeView.ItemTemplate> |
| 24 | + |
| 25 | + <syncfusion:SfTreeView.Nodes> |
| 26 | + <treeviewengine:TreeViewNode Content="Australia" IsExpanded="True"> |
| 27 | + <treeviewengine:TreeViewNode.ChildNodes> |
| 28 | + <treeviewengine:TreeViewNode Content="New South Wales"> |
| 29 | + <treeviewengine:TreeViewNode.ChildNodes> |
| 30 | + <treeviewengine:TreeViewNode Content="Sydney"/> |
| 31 | + <treeviewengine:TreeViewNode Content="Canberra"/> |
| 32 | + <treeviewengine:TreeViewNode Content="Newcastle–Maitland"/> |
| 33 | + </treeviewengine:TreeViewNode.ChildNodes> |
| 34 | + </treeviewengine:TreeViewNode> |
| 35 | + <treeviewengine:TreeViewNode Content="Victoria"> |
| 36 | + <treeviewengine:TreeViewNode.ChildNodes> |
| 37 | + <treeviewengine:TreeViewNode Content="Melbourne"/> |
| 38 | + </treeviewengine:TreeViewNode.ChildNodes> |
| 39 | + </treeviewengine:TreeViewNode> |
| 40 | + </treeviewengine:TreeViewNode.ChildNodes> |
| 41 | + </treeviewengine:TreeViewNode> |
| 42 | + </syncfusion:SfTreeView.Nodes> |
| 43 | + </syncfusion:SfTreeView> |
| 44 | + </StackLayout> |
| 45 | +``` |
| 46 | + |
| 47 | +### TextColorConverter |
| 48 | +```csharp |
| 49 | +public class TextColorConverter : IValueConverter |
| 50 | +{ |
| 51 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 52 | + { |
| 53 | + var level = (int)value; |
| 54 | + if (level == 0) return Color.Red; |
| 55 | + else if (level == 1) return Color.Blue; |
| 56 | + else return Color.Green; |
| 57 | + } |
| 58 | + |
| 59 | + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
| 60 | + { |
| 61 | + throw new NotImplementedException(); |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Requirements to run the demo |
| 67 | +Visual Studio 2017 or Visual Studio for Mac. |
| 68 | +Xamarin add-ons for Visual Studio (available via the Visual Studio installer). |
| 69 | + |
| 70 | +## Troubleshooting |
| 71 | +### Path too long exception |
| 72 | +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. |
| 73 | + |
| 74 | +## License |
| 75 | + |
| 76 | +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