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 6823019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:43:58+00:00 2026-05-26T21:43:58+00:00

I’m trying to build a TreeView in WPF that has categories. Basically the structure

  • 0

I’m trying to build a TreeView in WPF that has categories.

Basically the structure looks like this:

Root  
 |_
 | Cat A
 |_
 | Cat B
 |_
   Cat C

Each Category is bound to a separate observable collection. Most of my categories are very simple. The TreeViewItem that is the category has it’s ItemsSource set and there is no hierarchy. However, I do have a category which needs to be represented in a special hierarchy.

Image that “Cat C” in the above tree then becomes like this:

Servers
  |_
  | [SERVER A's DISPLAY NAME]
  |  |_
  |  | Namespaces
  |  |  |_
  |  |_   [NAMESPACE alpha's DISPLAY NAME]
  |  | Deployments
  |  |  |_
  |  |_   [DEPLOYMENT 1's DISPLAY NAME]
  |_   Configuration File
    [SERVER B's DISPLAY NAME]

Basically, what I want is the hard coded parent TreeViewItem to have a static header. That item should then have a collection of items with their names shown. For each child of the original parent there should be three static items which have a dynamic list of children.

Writing this out makes me think that this should be a very simplistic problem to solve. However, after playing with the XAML for a couple of days I cannot get a hierarchy to work. Below is the farthest I’ve been able to get. I’ve used a composite collection called Children to get the Namespaces, Deployments, and the Configuration File into one collection. However, I cannot separate them out.

<TreeViewItem ItemsSource="{Binding Path=Configuration.Servers}" 
                      IsExpanded="True" >
            <TreeViewItem.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
                    <Setter Property="IsExpanded" Value="True"/>
                </Style>
            </TreeViewItem.ItemContainerStyle>

            <TreeViewItem.HeaderTemplate>
                <DataTemplate>
                    <Border Margin="0,2,2,0">
                        <StackPanel Orientation="Horizontal">
                            <Image Source="/WPF;component/Images/server_chart.png"
                                   Margin="0,0,5,0"/>
                            <TextBlock Text="Cognos Servers" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </TreeViewItem.HeaderTemplate>

            <TreeViewItem.Resources>
                <HierarchicalDataTemplate ItemSource="{Binding Path=Children}" DataType="{x:Type local:Server}">
                    <HierarchicalDataTemplate.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}" >
                            <Setter Property="IsExpanded" Value="True"/>
                        </Style>
                    </HierarchicalDataTemplate.ItemContainerStyle>
                    <TextBlock Text="{Binding DisplayName}" PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown">
                        <TextBlock.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Refresh" Click="TreeItemMenu_AddNewClient">
                                    <MenuItem.Icon>
                                        <Image Source="/WPF;component/Images/arrow_refresh.png" />
                                    </MenuItem.Icon>
                                </MenuItem>
                                <MenuItem Header="Add Client..." Click="TreeItemMenu_AddNewClient" />
                                <Separator />
                                <MenuItem Header="Remove" Click="TreeItemMenu_RemoveClick">
                                    <MenuItem.Icon>
                                        <Image Source="/WPF;component/Images/server_delete.png" />
                                    </MenuItem.Icon>
                                </MenuItem>
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                    </TextBlock>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate ItemsSource="{Binding Clients}" DataType="{x:Type local:Namespace}">
                    <HierarchicalDataTemplate.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}" >
                            <Setter Property="IsExpanded" Value="True"/>
                        </Style>
                    </HierarchicalDataTemplate.ItemContainerStyle>
                    <TextBlock Text="{Binding DisplayName}" PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown" />
                </HierarchicalDataTemplate>

                <DataTemplate DataType="{x:Type local:Client}">
                    <TextBlock Text="{Binding DisplayName}"
                               ContextMenu="{StaticResource ResourceKey=ContextMenuTreeItem}" 
                               PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown" />
                </DataTemplate>
            </TreeViewItem.Resources>
  • 1 1 Answer
  • 0 Views
  • 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-26T21:43:58+00:00Added an answer on May 26, 2026 at 9:43 pm

    Mike,

    Mixing static and model data probably won’t get you where you want. You’ll have to create an actual ViewModel that contains the static nodes, etc.
    I highly recommend Josh Smith’s TreeView ViewModel tutorials. This is the first one, but look around for more info. He is one of the most knowledgeable developer on that subject. We had great success following his recommendations.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6

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.