tags:

views:

910

answers:

1

I am trying to make a WPF Application containing a treeview, whose data will be populated from the database. Since I am new to all this, I tried using the simple tutorial that can be found at http://dev102.blogspot.com/2007/12/how-to-use-hierarchical-datatemplate-in.html

I tried following the steps exactly, but all I am getting is just the root node. My xaml looks like this:

<Window x:Class="Hierarchical_attempt.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:data="clr-namespace:Hierarchical_attempt"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <HierarchicalDataTemplate DataType="{x:Type data:Root}"
                                  ItemsSource="{Binding Path=WebPages}">
            <TextBlock Text="{Binding Title}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type data:WebPage}"
                                  ItemsSource="{Binding Path=LinksInPage}">
            <TextBlock Text="{Binding PageTitle}" />
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <TreeView Name="tree1">
            <TreeViewItem ItemsSource="{Binding Path=Webpages}"
                          Header="{Binding Title}">
            </TreeViewItem>
        </TreeView>
    </Grid>
</Window>

Can you plese tell me where am I going wrong? I am just tring this since this will give me a heads up as to how to go about the treeview. However, what I actually have to do is to populate the data fom the database in the treeview. If you know of any step by step tutorial available, then that will also be really helpful. Thanks. Please reply soon.

A: 

It looks as though you have a typo on your binding path. It should be:

<TreeView>
  <TreeViewItem ItemsSource="{Binding Path=WebPages}"
                Header="{Binding Title}">
  </TreeViewItem>
</TreeView>

Note that the P in WebPages is in upper case.

If you are using Visual Studio to develop this, then you should examine the Output pane when running the application. Any binding errors such as this won't raise exceptions, but they will create helpful messages there.

I also simplified the XAML slightly in the original question.

Drew Noakes
As Pavel mentioned in the comment above, it would be helpful to see your code-behind to see how your data is being bound to the tree view.
Drew Noakes
Thank you sir, I indeed made a typo. I am just a beginner and am trying my best to get to learn. Anyway, it is working now. Can you please help me with the same thing, just the difference now being that I need to populate the tree data from the database. How should I approach that? Any sort of help will be eally beneficial!!!!
Gagan
here is my code behind:InitializeComponent();Root r = new Root();r.WebPages = new List<WebPage>();r.Title = "HomePage";r.Url = "google.com";WebPage link = new WebPage();link.Href = @"www.google.com/images";link.PageTitle = "Images";r.WebPages.Add(link);WebPage link2 = new WebPage();link2.Href = @"www.yahoo.com";link2.PageTitle = "YAHOO";r.WebPages.Add(link2);WebPage link3 = new WebPage();link3.Href = @"www.aol.com";link3.PageTitle = "AOL";link2.LinksInPage = new List<WebPage>();link2.LinksInPage.Add(link3);tree1.DataContext = r;
Gagan
Regarding your question about populating from DB. It deserves a separate question which many devs are coping with. Short story, get your data from the DB as you always get. now try to transform it to something that you can work with the UI. I would look at MVVM or MVP patterns to achieve a clean speration of concerns.
ArielBH