This is a migrated thread and some comments may be shown as answers.

a tree within a tree

4 Answers 83 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Iron
Stephen asked on 09 Oct 2013, 08:43 PM
Is there a simple, elegant method to add a treeView to a treeView in C# code (as opposed to the XAML)?

              <telerik:RadTreeView x:Name="rtvRoot" HorizontalAlignment="Left" Height="300" Margin="671,118,0,0" VerticalAlignment="Top" Width="300">
            <telerik:RadTreeViewItem DropPosition="Inside" Header="title images" Selected="RadTreeViewItem_Selected_1">
                <Button x:Name="test" Content="Jesper" Click="test_Click"/>
                <Button x:Name="test2" Content="Aaberg"/>
                <Button x:Name="last1" Content="12345"/>
            </telerik:RadTreeViewItem>
            <telerik:RadTreeViewItem DropPosition="Inside" Header="escrow images">
                <telerik:RadTreeView x:Name="etyps" Height="250" Width="250">
                    <telerik:RadTreeViewItem DropPosition="Inside" Header="deeds"/>
                    <telerik:RadTreeViewItem DropPosition="Inside" Header="reports">
                        <telerik:RadTreeView x:Name="eStyps" Height="200" Width="150">
                            <telerik:RadTreeViewItem x:Name="rtviSub1" DropPosition="After" Header="sub1"/>
                            <telerik:RadTreeViewItem DropPosition="After" Header="sub2"/>
                        </telerik:RadTreeView>
                    </telerik:RadTreeViewItem>
                </telerik:RadTreeView>
            </telerik:RadTreeViewItem>
        </telerik:RadTreeView>

I'm guessing the solution will be in the infamous binding technique...

4 Answers, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 09 Oct 2013, 09:13 PM
Hi Stephen,

You can examine the RadTreeView DataPopulation in Code-Behind tutorial to see how to populate the tree component with items in code-behind.

However, please have in mind that both the RadTreeView and the RadTreeViewItem classes derive from an ItemsControl and therefore they both provide Items collections. This is why I wouldn't recommend nesting RadTreeViews. In most cases, creating a hierarchy of RadTreeViewItems results in the very same layout. Please consider that option before building your tree layout and don't hesitate to write back if you have any concerns or questions on that matter.

Also, as you mentioned binding I just wanted to elaborate on the data-binding options available within the RadTreeView control. Basically as the component is data-driven, it is mostly used to display business collections. And although you can simply set its ItemsSource to any collection of business items, many developers use the MVVM pattern in their applications. This is why the ItemsSource property is designed to allow data-binding thus giving you the chance to easily build MVVM RadTreeView applications where you define the view entirely in XAML and the data in your business layer.

But if you'd rather manually populate the RadTreeView declaratively, the tutorial I first mentioned (RadTreeView DataPopulation in Code-Behind) should get you started. And if you have a business collection that you need to display in the RadTreeView control, you can set it as its ItemsSource and then apply ItemTemplates that control the visualization of the business items.

Let us know if you need more information.

Regards,
Tina Stancheva
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Stephen
Top achievements
Rank 1
Iron
answered on 09 Oct 2013, 09:35 PM
First, I am very much a beginner in Visual Studio and C#, and, of course Telerik controls. So, apologies if I am missing the point(s) - I have been working on this for several hours, and have been trying to research via topics found in general web searches as well as Telerik media (i.e. Telerik WPF Demos).

I looked at the example in you link,

The reason I thought, think, I need what is basically a treeview within a treeview is because, using the example above, I would want the Productx.y's to expand to another treeview, which would then finally actually have treeviewitems.
As, I mentioned before, I have it crudely done in my XAML, but I will want to build dynamically dependent on our database.

I am fine on adding items to the treeview's, from the XAMl or in code.

Thanks,
Stephen
0
Accepted
Tina Stancheva
Telerik team
answered on 11 Oct 2013, 06:38 PM
Hello Stephen,

Thank you for getting back to us. The RadTreeView can host as many levels of items as you need. You really don't need to nest TreeViews - basically as long as you keep populating the RadTreeViewItem Items/ItemsSource collections, the RadTreeView will keep displaying new levels in its hierarchy.

As these are your first steps with the control and WPF, I would definitely recommend taking an MVVM approach and taking advantage of the data-binding capabilities of RadControls. In the RadTreeView particularly, the ItemsSource property in both the RadTreeView and the RadTreeViewItem objects allows you to provide a business collection of entries and display it in the components.

so in your case, if you have a database with related tables, you can create a collection of records coming from the main table and set it as an ItemsSource of the RadTreeView. Then use HierarchicalDataTemplates to define the visualizaiton of the records from your main table. The HierarchicalDataTemplate has an ItemsSource property which also can be populated with business data. You need to use this property and bind it to the children collection coming from your table. This means that if you have a table of customers - you can create a collection of Customer objects and apply it on the RadTreeView ItemsSource. If every Customer record has a collection of Orders coming from another table, then you can use a HierarchicalDataTemplate as a RadTreeView ItemTemplate. The ItemsSource of the HierarchicalDataTemplate should be set to the Orders collection.

I'd recommend going through the following tutorials before going ahead with your application as they can help you choose the best approach for your particular scenario:

I hope this information can help you get started. Please don't hesitate to write back with any additional questions.

Regards,
Tina Stancheva
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Stephen
Top achievements
Rank 1
Iron
answered on 11 Oct 2013, 11:52 PM
Tina, thanks, I got it.

You are right, I could just do it with items, etc. It dawned on me the the other night. I have it building dynamically and quite elegantly, at least for a novice like me.
Tags
TreeView
Asked by
Stephen
Top achievements
Rank 1
Iron
Answers by
Tina Stancheva
Telerik team
Stephen
Top achievements
Rank 1
Iron
Share this question
or