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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:59:22+00:00 2026-05-25T17:59:22+00:00

I would like to take a flat list of objects and present them in

  • 0

I would like to take a flat list of objects and present them in a TreeView using custom groups.

public enum DocumentType { Current, Inactive, Transition, Checkpack, TechLog, Delivery }
public enum Status { Approved, Rejected, Pending }

public class Document
{
    public string Name { get; set; }
    public DateTime Created { get; set; }
    public string CreatedBy { get; set; }
    public DateTime Modified { get; set; }
    public string ModifiedBy { get; set; }
    public DocumentType Type { get; set; }
    public Status Status { get; set; }
}

For example… The user might want to see this list, with the top level group being “Status” and the second level being “Name”. This all needs to be configurable from the UI, and I’m struggling to find the best way to achieve it.

I’ve had a brief look at the CollectionViewSource object, but couldn’t find a good way to get it to dynamically build a TreeView.

My gut feeling is that i’ll need to do some clever templating in XAML – this is as far as i’ve got…

<Window.Resources>
    <DataTemplate x:Key="DocumentTemplate">
        <DockPanel>
            <TextBlock Text="{Binding Name}" />
        </DockPanel>
    </DataTemplate>

    <HierarchicalDataTemplate x:Key="GroupTemplate"
                              ItemsSource="{Binding Path=Items}"
                              ItemTemplate="{StaticResource DocumentTemplate}">
        <TextBlock Text="{Binding Path=Name}" />
    </HierarchicalDataTemplate>
</Window.Resources>
<Grid>
    <TreeView ItemsSource="{Binding Documents.View.Groups}"
              ItemTemplate="{StaticResource GroupTemplate}"/>
</Grid>


    public CollectionViewSource Documents
    {
        get
        {
            var docs = new CollectionViewSource();
            docs.Source = DocumentFactory.Documents;
            docs.GroupDescriptions.Add(new PropertyGroupDescription("CreatedBy"));
            return docs;
        }
    }

Of course this only displays the Top-level group (“CreatedBy”).


After reading a question below, I managed to come up with a better question…

My question: Is it possible to have a generic HierarchicalDataTemplate for a TreeView that displays custom groups applied to a CollectionViewSource.

  • 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-25T17:59:23+00:00Added an answer on May 25, 2026 at 5:59 pm

    Honestly this should be marked as a bug in WPF. I too tried and found that Documents.View.Groups throws binding error on View property being null.

    Also

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

    is correct in the GroupTemplate but not in the DocumentTemplate. Note that Groups are of special type GroupItem where Name is one such property that holds the value on which grouping has taken place.

    On the other hand in DocumentTemplate, we should refer the property that we need to display on the leaf nodes items e.g. in my example I used Employee.FirstName (I grouped on Gender).

     <DataTemplate x:Key="DocumentTemplate">
          <DockPanel>
              <TextBlock Text="{Binding FirstName}" />
          </DockPanel>
     </DataTemplate>
    

    Now for binding to take effect I had to introduce a converter which simply returns Groups.

    public class GroupsConverter : IValueConverter
    {
        public object Convert(object value, ...)
        {
            return ((CollectionViewSource)value).View.Groups;
        }
    
        ....
    }
    

    And tree view binding was changed this way…

    <TreeView x:Name="treeView"                  
              ItemsSource="{Binding Path=Documents,
                                    Converter={StaticResource GroupsConverter}}"
              ItemTemplate="{StaticResource GroupTemplate}" />
    

    Then this worked for me.

    Does this help you?

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

Sidebar

Related Questions

Using Flex 3, I would like to take an image snapshot such as this:
Using sql server 2000, I would like to take my production data and put
I am working on a project using Groovy, and I would like to take
I would like to take requests for /somefolder/ style.css and handle them with /somefolder/
I would like to take a list of names (e.g. john, mary, paul) and
I would like to take bites out of a list (or array) of a
I would like to take a pascal-cased string like CountOfWidgets and convert it into
I would like to take a stack of images (or potentially an array of
I would like to take a string representation of a set and parse it.
I would like to take a field and replace all characters that are not

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.