diff --git a/README.md b/README.md index 5d41781..66b59a0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,59 @@ -# How to customize tree nodes using data template selector in wpf treeview? -This repository describes how to customize tree nodes using data template selector in wpf treeview +# How to customize tree nodes using data template selector in WPF TreeView + +This repository describes how to customize tree nodes using data template selector in [WPF TreeView](https://www.syncfusion.com/wpf-controls/treeview) (SfTreeView) + +The TreeView allows you to customize the appearance of each item with different templates based on specific constraints by using the [ItemTemplateSelector](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeView.SfTreeView.html#Syncfusion_UI_Xaml_TreeView_SfTreeView_ItemTemplateSelector). You can choose a [DataTemplate](https://docs.microsoft.com/en-us/dotnet/api/system.windows.datatemplate?view=netcore-3.1) for each item at runtime based on the value of data-bound property using `ItemTemplateSelector`. + +#### XAML + +``` xml + + + + + + + + + + + + + + + + + + +``` + +#### C# + +``` csharp +class ItemTemplateSelector : DataTemplateSelector +{ + public override DataTemplate SelectTemplate(object item, DependencyObject container) + { + var treeviewNode = item as TreeViewNode; + if (treeviewNode == null) + return null; + if (treeviewNode.Level == 0) + return Application.Current.MainWindow.FindResource("RootTemplate") as DataTemplate; + else + return Application.Current.MainWindow.FindResource("ChildTemplate") as DataTemplate; + } +} +``` \ No newline at end of file