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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:57:03+00:00 2026-05-26T01:57:03+00:00

I have a class like this : public class Project { List<Object> list1; List<Object>

  • 0

I have a class like this :

public class Project
{
List<Object> list1;
List<Object> list2;
}

I want to show this in a treeview control like as follows :

Checkbox + "Listing1"
--> Checkbox + Object 1 of list1
--> Checkbox + Object 2 of list1
Checkbox + "Listing2"
--> Checkbox + Object 1 of list2
-->Checkbox + Object 2 of list2

My biggest problem is making the difference between the 2 lists + some extra : if list2 does not contain any objects, the “Listing2” header may not be shown.

Does anybody have any good idea how I can do this ?

  • 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-26T01:57:04+00:00Added an answer on May 26, 2026 at 1:57 am

    You can create one class TreeViewItemWithCheckbox by extending TreeViewItem class like below :

    public class TreeViewItemWithCheckbox : TreeViewItem
        {
            #region Variable Declaration
    
            CheckBox chkBox = new CheckBox();
            StackPanel stpContent = new StackPanel();
    
            #endregion
    
            #region Properties
    
            public string HeaderText
            {
                get { return chkBox.Content.ToString(); }
                set { chkBox.Content = value; }
            }
    
            public bool IsChecked
            {
                get { return chkBox.IsChecked.Value; }
                set { chkBox.IsChecked = value; }
            }
    
            #endregion
    
            #region Constructor
    
            public TreeViewItemWithCheckbox()
            {
                stpContent.Orientation = Orientation.Horizontal;
    
                chkBox = new CheckBox();
                chkBox.VerticalAlignment = VerticalAlignment.Center;
                chkBox.Click += new RoutedEventHandler(SetCheckboxes);
                chkBox.Margin = new Thickness(0, 0, 0, 0);
                stpContent.Children.Add(chkBox);
    
                Header = stpContent;
            }
    
            #endregion
    
            #region Event Handlers
    
            private void SetCheckboxes(object sender, RoutedEventArgs e)
            {
                TreeViewItemWithCheckbox selectedItem = ((TreeViewItemWithCheckbox)((StackPanel)((CheckBox)sender).Parent).Parent);
    
                if (selectedItem != null)
                {
                    /* Set checkboxes for all child items */
                    if (selectedItem.Items.Count > 0)
                    {
                        SetChildItemCheckboxes(selectedItem, selectedItem.IsChecked);
                    }
    
                    /* Check if all childs checked then check/uncheck parent accoringly */
                    if (selectedItem.Parent.GetType() == typeof(TreeViewItemWithCheckbox))
                    {
                        TreeViewItemWithCheckbox parentItem = (TreeViewItemWithCheckbox)selectedItem.Parent;
    
                        bool bIsAllChecked = true;
                        foreach (TreeViewItemWithCheckbox item in parentItem.Items)
                        {
                            if (!item.IsChecked)
                            {
                                bIsAllChecked = false;
                                break;
                            }
                        }
                        parentItem.IsChecked = bIsAllChecked;
                    }
                }
            }
    
            private void SetChildItemCheckboxes(TreeViewItemWithCheckbox item, bool IsChecked)
            {
                if (item.Items.Count > 0)
                {
                    foreach (TreeViewItemWithCheckbox childItem in item.Items)
                    {
                        SetChildItemCheckboxes(childItem, IsChecked);
                        item.IsChecked = IsChecked;
                    }
                }
                else
                    item.IsChecked = IsChecked;
            }
    
            #endregion
        }
    

    Then you need to add treeview nodes from 2 list like below :

    trvTest.Items.Clear();
    
    //Add default root element
    TreeViewItemWithCheckbox rootNode = new TreeViewItemWithCheckbox();
    rootNode.HeaderText = "Root";
    rootNode.IsExpanded = true;
    trvTest.Items.Add(rootNode);
    
    if (_project.list1.Count > 0)
    {
        TreeViewItemWithCheckbox nodeHead1 = new TreeViewItemWithCheckbox();
        nodeHead1.HeaderText = "Listing 1";
        rootNode.Items.Add(nodeHead1);
    
        TreeViewItemWithCheckbox node1;
        for (int i = 0; i < _project.list1.Count; i++)
        {
           node1 = new TreeViewItemWithCheckbox();
           node1.HeaderText = _project.list1[i].Name;
           nodeHead1.Items.Add(node1);
        }
    }
    
    if (_project.list2.Count > 0)
    {
        TreeViewItemWithCheckbox nodeHead2 = new TreeViewItemWithCheckbox();
        nodeHead2.HeaderText = "Listing 2";
        rootNode.Items.Add(nodeHead2);
    
        TreeViewItemWithCheckbox node2 = new TreeViewItemWithCheckbox();
        for (int i = 0; i < _project.list2.Count; i++)
        {
           node2 = new TreeViewItemWithCheckbox();
           node2.HeaderText = _project.list2[i].Name;
           nodeHead2.Items.Add(node2);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like this: public class myClass { public List<myOtherClass> anewlist =
I have a class like this: public class Contest { List<ContestTeam> Teams { get;
I have a class like this: public class Stretcher : Panel { public static
I have a class like this public class foo { private void getThread() {
i am having a strange problem, i have a static class like this public
I have a class that looks like this public class SomeClass { public SomeChildClass[]
I have a class that looks like this: public class TextField : TextBox {
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have a class that looks like this: public class BasePadWI { public WorkItem
I have a class like this class GUI : public QWidget, public QThread When

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.