Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everybody.
I am doing a project with WPF, it's the first time, so i am not an expert in WPF, and i have some problems with treeView.
I am trying to create a treeView from an instance of my class menu. A menu is composed by Items and every Item can have a collection of subItems, which can also have subItems and so on...

I am initializing my menu and everything is right, but when i try to use the dataBinding to show the content of my menu in a treeView i have many problems, and i am searching for a solution, but didn't find anything.

Here is my xaml code:

XML
<Grid.Resources>
            <HierarchicalDataTemplate x:Key="Temp">
                <TreeViewItem  ItemsSource="{Binding Path=ViewModelPropertyMenu.it/Subitems, Source={StaticResource Locator}}" Header="{Binding Path=ViewModelPropertyMenu.it/Label, Source={StaticResource Locator}}"  />
            </HierarchicalDataTemplate>

        </Grid.Resources>


....

<pre lang="xml"><TreeView  AllowDrop="True"  >

                <TreeViewItem Header="{Binding Path=Name}" ItemTemplate="{StaticResource Temp}" ItemsSource="{Binding Path=it, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />


            </TreeView>



I am using the patterns MVVM and Locator, so The class locator is used to locate the ViewModel Classes i am using.

The treeView generated shows three items which have the same name (the name of the first item, and my menu is really composed of three items) then for every Item, it shows 11 subItems ( only my first Item have 11 SubItems) but it shows them under this form "Project.Model.Items"

Thanks in Advance. I am really lost. It has been a week since i am searching for a solution. :sigh:
Posted
Updated 12-Jul-10 3:51am
v2

I'm working with Telerik controls and had the same problem. Found tricky solution with proper ValueConverter applied to certain items collection at http://blogs.telerik.com/valerihristov/posts/09-08-26/self-reference_hierarchy_with_telerik_treeview_for_silverlight.aspx. I don't know if this solution can be used with common TreeView. Most things looks similar so maybe you can find something usefull.
 
Share this answer
 
Thanks helianthus87 for responding, but it is not the same for me.
 
Share this answer
 
Comments
helianthus87 13-Jul-10 3:09am    
That was quick tricky solution i found accidently. Before that i was thinking about universial solution. Recursive adding items in code behind could be an option. You need some function like AddNode(Node root) where you call something like:
foreach(Node n in Nodes)
{
AddNode(n);
}
I wrote something like this to draw genealogical tree from list of Nodes where each node had ParentId, ChildId etc. so the sturcture is similar. Don't really know how to combine it with HierarchicalDataTemplate and TreeView but I'll try to figure out something after work. If i find this thing (its very old, dirty and messy piece of code ;P) i can send it to you via email or something.

About my previous solution... I'm almost sure that you can do this the same way using Proper IValueConverter to bounded items.
I discovered why i had the same name for every Item. In fact i was using different DataContext for the HierarchicalDataTemplate and the TreeView.
Now it shows me the real names of my items.
But, i still have two problems:
1) The subitems still under this form (Project.Model.Items)
2) How i should declare my HierarchicalDataTemplate to have all the subitems of my items until there is no more subitems.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900