TreeViewControl.NodeTemplateSelector Property
Gets or sets an object that chooses a template for data nodes based on custom logic. This is a dependency property.
Namespace: DevExpress.Xpf.Grid
Assembly: DevExpress.Xpf.Grid.v25.1.dll
NuGet Package: DevExpress.Wpf.Grid.Core
Declaration
Property Value
| Type | Description |
|---|---|
| DataTemplateSelector | A DataTemplateSelector descendant that chooses a template based on custom logic. |
Remarks
The TreeViewControl.NodeTemplate property allows you to specify a template that changes the entire default appearance of data nodes. You can use the TreeListRowData object to get information about a node.
The TreeViewControl.NodeTemplate property specifies a template that defines the appearance of data nodes.
Note
Use the TreeViewControl.NodeContentTemplate property to specify a template that defines the appearance of node content.
If you have multiple node templates, use the TreeViewControl.NodeTemplateSelector property to implement custom logic that chooses the template:
- Create a class that chooses a template based on the condition. This class should derive from the DataTemplateSelector class.
- Override the SelectTemplate method to return a template that meets the required condition.
- Assign the instance of the template selector class to the
NodeTemplateSelectorproperty.
public class MyTemplateSelector : DataTemplateSelector
{
public DataTemplate DefaultTemplate { get; set; } = null!;
public DataTemplate CustomTemplate { get; set; } = null!;
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is TreeListRowData rowData && rowData.Row is TreeNode node && node.IsCustom)
{
return CustomTemplate;
}
return DefaultTemplate;
}
}
<Window.Resources>
<DataTemplate x:Key="DefaultNodeTemplate">
<TextBlock Text="{Binding Row.Content}" FontWeight="Bold"/>
</DataTemplate>
<DataTemplate x:Key="CustomNodeTemplate">
<TextBlock Text="{Binding Row.Content}" FontStyle="Italic"/>
</DataTemplate>
<local:MyTemplateSelector x:Key="NodeTemplateSelector"
DefaultTemplate="{StaticResource DefaultNodeTemplate}"
CustomTemplate="{StaticResource CustomNodeTemplate}" />
</Window.Resources>
<Grid>
<dxg:TreeViewControl ItemsSource="{Binding TreeNodes}"
ChildNodesPath="Children"
NodeTemplateSelector="{StaticResource NodeTemplateSelector}"/>
</Grid>
If you specify both TreeViewControl.NodeTemplate and NodeTemplateSelector, the TreeViewControl uses the template returned by the template selector.
If the template selector returns null, the TreeViewControl uses the template specified by the TreeViewControl.NodeTemplate property.