Articles in this section
Category / Section

How to generate tree like diagram with nested objects in WPF ?

1 min read

Nested business objects are directly set as ItemSource and a tree diagram is generated automatically by using the HierarchicalDataTemplate. The following code example explains how to use the HierarchicalDataTemplate.

C#

public class NodeHead
{
    public NodeHead()
    {
       Children = new ObservableCollection<NodeHead>();
    }
    public ObservableCollection<NodeHead> Children { get; set; } 
    public string Name { get; set; }
}

 

XAML

<DataTemplate x:Key="NodeTemplate">
            <Border Background="{StaticResource OuterBrush}" CornerRadius="10" IsHitTestVisible="False">
                <Border Margin="10" BorderBrush="Black" BorderThickness="2" CornerRadius="20" Background="{StaticResource InnerBrush}">
                    <StackPanel Orientation="Vertical">
                        <TextBlock FontSize="10" HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="White" Text="{Binding Path=Name}" />
                    </StackPanel>
                </Border>                
            </Border>
</DataTemplate>
<Style TargetType="{x:Type syncfusion:Node}" x:Key="{x:Type syncfusion:Node}">
                <Setter Property="Width" Value="75" />
                <Setter Property="Height" Value="50" />
                <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                <Setter Property="ContentTemplate" Value="{StaticResource NodeTemplate}" />
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Name}" />
</Style>
<HierarchicalDataTemplate x:Key="dataTemplate"ItemsSource="{Binding Path=Children}"    DataType="{x:Type local:NodeHead}"></HierarchicalDataTemplate>

 

C:\Users\saranya\Desktop\hier.jpg

Figure 1: Hierarchical Data

Sample References:

Diagram WPF: Dashboard > WPF Sample Browser > Diagram > DataBinding > Business Object Binding Sample.

Diagram Silverlight: Dashboard > Silverlight Sample Browser > Diagram > DataBinding > Business Object Binding Sample.


Conclusion

I hope you enjoyed learning about how to generate tree like diagram with nested objects in WPF.

You can refer to our WPF Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied