Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 496271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:40:14+00:00 2026-05-13T05:40:14+00:00

I have a collection of Database objects, each containing collections of Schema objects and

  • 0

I have a collection of Database objects, each containing collections of Schema objects and User objects. I want to bind them to a TreeView, but adding additional static levels in the hierarchy, so that the resulting TreeView looks more or less like this:

<TreeView>
    <TreeViewItem Header="All the databases:">
        <TreeViewItem Header="Db1">
            <TreeViewItem Header="Here's all the schemas:">
                <TreeViewItem Header="Schema1"/>
                <TreeViewItem Header="Schema2"/>
            </TreeViewItem>
            <TreeViewItem Header="Here's all the users:">
                <TreeViewItem Header="User1"/>
                <TreeViewItem Header="User2"/>
            </TreeViewItem>
        </TreeViewItem>
        <TreeViewItem Header="Db2">
            <TreeViewItem Header="Here's all the schemas:">
                <TreeViewItem Header="Schema1"/>
                <TreeViewItem Header="Schema2"/>
            </TreeViewItem>
            <TreeViewItem Header="Here's all the users:">
                <TreeViewItem Header="User1"/>
                <TreeViewItem Header="User2"/>
            </TreeViewItem>
        </TreeViewItem>
    </TreeViewItem>
</TreeView>

I was able to get pretty close to what I want by using the following templates:

<Window.Resources>
    <HierarchicalDataTemplate DataType="{x:Type smo:Database}">
        <TreeViewItem Header="{Binding Path=Name}">
            <TreeViewItem Header="Here's all the schemas:" ItemsSource="{Binding Path=Schemas}"/>
            <TreeViewItem Header="Here's all the users:" ItemsSource="{Binding Path=Users}"/>
        </TreeViewItem>
    </HierarchicalDataTemplate>
    <DataTemplate DataType="{x:Type smo:Schema}">
        <TextBlock Text="{Binding Path=Name}"/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type smo:User}">
        <TextBlock Text="{Binding Path=Name}"/>
    </DataTemplate>
</Window.Resources>

Then in the code I set the binding like this:

TreeViewItem treeViewItem = new TreeViewItem();
treeViewItem.Header = "All the databases:";
treeViewItem.ItemsSource = server.Databases;
treeView.Items.Add(treeViewItem);

The resulting TreeView looks like I want it to, but it’s not possible to select a particular schema or user. Apparently WPF sees the whole subtree rooted at a database node as a single item, and it only selects the whole thing. I need to be able to select a particular schema, user or database. How do I set the templates and bindings so that it works the way I need?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:40:14+00:00Added an answer on May 13, 2026 at 5:40 am

    Oh man this is an incredibly frustrating task. I’ve tried doing it myself many times. I had a very similar requirement where I’ve got something like a Customer class that has both a Locations collection and a Orders collection. I wanted Locations and Orders to be “folders” in the tree view. As you’ve discovered, all the TreeView examples that show you how to bind to self-referencing types are pretty much useless.

    First I resorted to manually building a tree of FolderItemNode and ItemNode objects that I would generate in the ViewModel but this defeated the purpose of binding because it would not respond to underlying collection changes.

    Then I came up with an approach which seems to work pretty well.

    • In the above described object model, I created classes LocationCollection and OrderCollection. They both inherit from ObservableCollection and override ToString() to return “Locations” and “Orders” respectively.
    • I create a MultiCollectionConverter class that implements IMultiValueConverter
    • I created a FolderNode class that has a Name and Items property. This is the placeholder object that will represent your “folders” in the tree view.
    • Define hierarchicaldatatemplate’s that use MultiBinding anywhere that you want to group multiple child collections into folders.

    The resulting XAML looks similar to the code below and you can grab a zip file which has all the classes and XAML in a working example.

    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:Local="clr-namespace:WpfApplication2"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    
        <Window.Resources>
    
            <!-- THIS IS YOUR FOLDER NODE -->
            <HierarchicalDataTemplate DataType="{x:Type Local:FolderNode}" ItemsSource="{Binding Items}">
                <Label FontWeight="Bold" Content="{Binding Name}" />
            </HierarchicalDataTemplate>
    
            <!-- THIS CUSTOMER HAS TWO FOLDERS, LOCATIONS AND ORDERS -->
            <HierarchicalDataTemplate DataType="{x:Type Local:Customer}">
                <HierarchicalDataTemplate.ItemsSource>
                    <MultiBinding>
                        <MultiBinding.Converter>
                            <Local:MultiCollectionConverter />
                        </MultiBinding.Converter>
                        <Binding Path="Locations" />
                        <Binding Path="Orders" />
                    </MultiBinding>
                </HierarchicalDataTemplate.ItemsSource>
                <Label Content="{Binding Name}" />
            </HierarchicalDataTemplate>
    
            <!-- OPTIONAL, YOU DON'T NEED SPECIFIC DATA TEMPLATES FOR THESE CLASSES -->
            <DataTemplate DataType="{x:Type Local:Location}">
                <Label Content="{Binding Title}" />
            </DataTemplate>
            <DataTemplate DataType="{x:Type Local:Order}">
                <Label Content="{Binding Title}" />
            </DataTemplate>
    
        </Window.Resources>
    
        <DockPanel>
            <TreeView Name="tree" Width="200" DockPanel.Dock="Left" />
            <Grid />
        </DockPanel>
    
    </Window>
    

    Folders in TreeView

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 339k
  • Answers 339k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Parameterise map with a custom comparator: struct dereference_compare { template… May 14, 2026 at 4:31 am
  • Editorial Team
    Editorial Team added an answer What version of jQuery are you using? parseJSON was added… May 14, 2026 at 4:31 am
  • Editorial Team
    Editorial Team added an answer The possibility is GStreamer, the concept is similar to DirectShow.… May 14, 2026 at 4:31 am

Related Questions

I've just started using NHibernate, and I have some issues that I'm unsure how
I have been programming in C# and Java for a little over a year
I am working on the redesign of an existing class. In this class about
I have a collection of objects in a database. Images in a photo gallery,
I have a collection of ActiveRecord objects. I want to be able to run

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.