• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
.NET Framework Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > How to populate a WPF TreeView node from a datasource?
Ask a questionAsk a question
Search Forums:
  • Search Windows Presentation Foundation (WPF) Forum Search Windows Presentation Foundation (WPF) Forum
  • Search All .NET Development Forums Search All .NET Development Forums
  • Search All MSDN Forums Search All MSDN Forums
 

Proposed AnswerHow to populate a WPF TreeView node from a datasource?

  • Tuesday, October 10, 2006 9:21 PMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    I'm having a question about populating a treeview control node.

      <TreeView>
        <TreeViewItem Header="Node1">
          <TreeViewItem Header="Subnode1"/>
           ...
          <TreeViewItem Header="Subnode10"/>

        </TreeViewItem>
        <TreeViewItem Header="Node2">
          <TreeViewItem Header="Subnode1"/>
           ...
          <TreeViewItem Header="Subnode10"/>

        </TreeViewItem>
      </TreeView>

    Now, my problem is that I have a observableCollection that holds infrmation for subnodes that go into "Node1", and another observableCollection that holds information about subnodes that go into "Node2". Until runtime, I don't know which elements will be in the node1 nor how many of them will be... Does anyone know how can I bind the information from ObservableCollections to the nodes/subnodes?

    Thanks in advance,
    Marko Vuksanovic.

    • ReplyReply
    • QuoteQuote
     

All Replies

  • Wednesday, October 11, 2006 8:19 AMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Also an example of how to programatically populate a TreeView control would be very useful.......
    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 8:29 AMlee dModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Did you try something like this

    <Window.Resources>

    <ObjectDataProvider ObjectType="{x:Type loc:MyData}" x:Key="odp1"></ObjectDataProvider>

    </Window.Resources>

    <TreeView>

    <TreeViewItem Header="Node1" ItemsSource="{Binding Source={StaticResource odp1}, Path=Data}">

    </TreeViewItem>

    <TreeViewItem Header="Node2">

    </TreeViewItem>

    </TreeView>

    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 8:47 AMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    I have done the following:

     <TreeViewItem Header="Sets" x:Name="SetsTreeViewItem" ItemsSource="{Binding Source={StaticResource PhotoSetCollectionDS}, Path=Title}">

    <ObjectDataProvider x:Key="PhotoSetCollectionDS" d:IsDataSource="True" ObjectType="{x:Type FlickrAPI:LeftMenuExpanderListboxItemsCollection}"/>

    But this does not populat the treeview node,.. it is empty,... If I populate a listbox from the above datasource it, the data is displayed correctly...

    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 9:03 AMlee dModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    It cannot find 'Title'  property

    try this

    <DataTemplate x:Key="dt">

    <TextBlock Text="{Binding Path=Title}"></TextBlock>

    </DataTemplate>

    <TreeViewItem Header="Sets"

    ItemTemplate="{StaticResource dt}"

    x:Name="SetsTreeViewItem" ItemsSource="{Binding Source={StaticResource PhotoSetCollectionDS}, Path=Title}">

    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 9:05 AMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Vote As Helpful
    0

    I also made sure that the datasource is not empty,... Still the data is not populated in the dataset.

    Now I realized that there might be one more problem. If the dataset is empty and there are not treeview items to be put in a treeview node the node will not be possible to expand... What I would like to make is that the data is fetched from the internet once the treeviewnode is expanded. Is this possible with the treeview control?

    • Proposed As Answer byprasad22MVPFriday, January 16, 2009 3:21 AM
    •  
    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 9:06 AMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    The title property is a property of my observable collection.... Did I do something wrong?
    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 9:21 AMlee dModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    if I understand correctly, source is a collection of some objects and Title is a property on the object right?
    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 9:34 AMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    That's right....

    If I populate the a ListBox control using the following code it work.... But I would like this data (that is now in the listbox) to be in a treview node....

         <ListBox Width="Auto" Height="Auto" x:Name="LeftMenuSetsExpanderListBox" SelectionChanged="OnLeftMenuItemSelected" Background="{x:Null}" BorderThickness="0,0,0,0" Padding="15,0,0,0" ItemsSource="{Binding PhotoSets, Mode=Default, Source={StaticResource PhotoSetCollectionDS}}" ItemTemplate="{DynamicResource PhotoSetsTemplate1}" LostFocus="OnListBoxLostFocus"/>

      <ObjectDataProvider x:Key="PhotoSetCollectionDS" d:IsDataSource="True" ObjectType="{x:Type FlickrAPI:LeftMenuExpanderListboxItemsCollection}"/>
        <DataTemplate x:Key="PhotoSetsTemplate1">
       <StackPanel Margin="0,0,0,0" Width="Auto" Height="Auto" Background="{x:Null}" x:Name="StackPanel" HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="0">
        <TextBlock Height="Auto" Width="Auto" x:Name="ButtonInExtender" Text="{Binding Title}" TextAlignment="Left"/>
       </StackPanel>

    Now I would like to populate the data from the aboove DataObjectProvide into a treeView node... but cannot figure out a way...

    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 9:36 AMlee dModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    did you try setting the itemtemplate on the treeviewnode like I mentioned?
    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 12:54 PMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Yes, I did and it did not work... i also made sure that the objectdataprovider/observable collection is not empty,.... I still cannot figure out a way to populate the treeview control...
    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 1:02 PMlee dModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Do you see any Binding Errors in the output window
    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 1:34 PMmarkovuksanovic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    No, there are no errors in the output.

    I also tried using the same binding as I used for the listbox but... it populates the listbox but not the treeview control...

         <TreeViewItem Header="Sets" IsExpanded="True" ItemTemplate="{StaticResource PhotoSetsTemplate2}" x:Name="SetsTreeViewItem" ItemsSource="{Binding PhotoSets, Mode=Default, Source={StaticResource PhotoSetCollectionDS}}">

    • ReplyReply
    • QuoteQuote
     
  • Wednesday, October 11, 2006 3:30 PMMike Hillberg - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    You likely want to use a HierarchicalDataTemplate for the TreeView.ItemTemplate; the ItemsSource property on that template is what describes the nested collection to the TreeView.

    I posted some examples here, and Bea has lots of other HierarchicalDataTemplates examples on her blog.

     

    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement