0
votes

i need to bind recursive child nodes for a treeview in wpf .But i really dunno how to make it work .Here is what i have found

ObservableCollection<Animal> animals = new ObservableCollection<Animal>();
animals.Add(new Animal("California Newt"));
animals.Add(new Animal("Tomato Frog"));
animals.Add(new Animal("Green Tree Frog"));
animals.Add(new Animal("Frog"));
AnimalCategories.Add(new AnimalCategory("Amphibians", animals));

animals = new ObservableCollection<Animal>();
animals.Add(new Animal("Golden Silk Spider"));
animals.Add(new Animal("Black Widow Spider"));
AnimalCategories.Add(new AnimalCategory("Spiders", animals))

List that need to be binded on the treeview and here is the structure for it

AnimalCategory Class:

Class AnimalCategory 
    Private _category As String
    Public Property Category As String
        Get
            Return _category
        End Get
        Set(ByVal value As String)
            _category = value
        End Set
    End Property
    Private _animals As ObservableCollection(Of Animal)
    Public ReadOnly Property Animals() As ObservableCollection(Of Animal)
        Get

            If _animals Is Nothing Then
                _animals = New ObservableCollection(Of Animal)()
            End If

            Return _animals
        End Get
    End Property

    Public Sub New()
    End Sub
    Public Sub New(category As String, animals As ObservableCollection(Of Animal))
        _category = category
        _animals = animals
    End Sub
End Class

Animal Class:

Class Animal
    Private _name As String
    Public Property Name As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set


    End Property
    Public Sub New()
    End Sub
    Public Sub New(name As String)
        _name = name
    End Sub
End Class

and my xaml code is..!

<TreeView x:Name="radTreeView"   Background="#4E4E4E" x:FieldModifier="public"  ItemsSource="{x:Static local:cntlWPFMMTreeView.AnimalCategories}"
        ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible">
<telerik:RadTreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Animals}">
            <TextBlock  Text="{Binding Category}" />
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </telerik:RadTreeView.ItemTemplate>
</TreeView>

Using the above code i can bind one level of parent child hiereachy like below,

enter image description here

Buit i need to get bind something like many level of hierarchy like below dynamically

enter image description here

How to do it in WPF ..?

2

2 Answers

0
votes

You can bind the value of one control with datacontext of another control as-

Binding="{Binding Path=DataContext.MyBindingProperty, ElementName=MyMainWindow}

Modify this line as per your requirement.

Hope it helps!

0
votes

Two things: You have to add an ObservableCollections of Animals to your Animal-Class. And you have to change the HierarchicalDataTemplate's ItemTemplate to another HierarchicalDataTemplate with ItemsSource="{Binding Animals}".