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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:15:06+00:00 2026-05-15T08:15:06+00:00

I have a custom class that I would like to bind a WPF TreeView

  • 0

I have a custom class that I would like to bind a WPF TreeView that has three tiers. Each tier needs to be bound like this:

    Monitor 
     --> LCD
          --> Samsung 1445 LCD
     --> CRT
          --> Sony 125 CRT

Here is the example code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        SystemInventory sysInventory = new SystemInventory();
        //Possibly do something like this.
        _myTreeView.DataContext = sysInventory.DeviceGroupInstances; 
    }

    public class SystemInventory
    {
        public ObservableCollection<DeviceGroup> DeviceGroupInstances { get; set; }

        public SystemInventory()
        {
            DeviceGroupInstances = new ObservableCollection<DeviceGroup>();
            DeviceGroupInstances.Add(new DeviceGroup("Monitor"));
        }
    }

    public class DeviceGroup
    {
        public string DeviceGroupName { get; set; }
        public ObservableCollection<DeviceType> DeviceTypeInstances { get; set; }

        public DeviceGroup(string deviceGroupName)
        {
            DeviceTypeInstances = new ObservableCollection<DeviceType>();
            DeviceGroupName = deviceGroupName;

            if (deviceGroupName == "Monitor")
            {
                DeviceTypeInstances.Add(new DeviceType("LCD"));
                DeviceTypeInstances.Add(new DeviceType("CRT"));
            }               
        }
    }

    public class DeviceType
    {
        public string DeviceTypeName { get; set; }
        public ObservableCollection<DeviceInstance> DeviceInstances { get; set; }

        public DeviceType(string deviceGroupName)
        {
            DeviceInstances = new ObservableCollection<DeviceInstance>();
            DeviceTypeName = deviceGroupName;

            if (deviceGroupName == "Monitor")
            {
                DeviceInstances.Add(new DeviceInstance("Samsung 1445 LCD"));
            }
            else
            {
                DeviceInstances.Add(new DeviceInstance("Sony 125 CRT"));
            }                
        }
    }

    public class DeviceInstance
    {
        public string DeviceInstanceName { get; set; }

        public DeviceInstance(string instanceName)
        {
            DeviceInstanceName = instanceName;
        }
    }
}
  • 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-15T08:15:07+00:00Added an answer on May 15, 2026 at 8:15 am

    First FIX

    Change CTOR:

    public MainWindow()
        {
            InitializeComponent();
            SystemInventory sysInventory = new SystemInventory();
            //Possibly do something like this.
            this.Content = sysInventory;
        }
    

    Second FIX

    Use XAML below (add to MainWindow), where {x:Type pc:MainWindow+SystemInventory} used for nested classes

    <Window.Resources>
        <DataTemplate DataType="{x:Type pc:MainWindow+SystemInventory}">
            <TreeView ItemsSource="{Binding DeviceGroupInstances}"/>
        </DataTemplate>
    
        <HierarchicalDataTemplate DataType="{x:Type pc:MainWindow+DeviceGroup}" ItemsSource="{Binding DeviceTypeInstances}">
            <Label Content="{Binding DeviceGroupName}"/>
        </HierarchicalDataTemplate>
    
        <HierarchicalDataTemplate DataType="{x:Type pc:MainWindow+DeviceType}" ItemsSource="{Binding DeviceInstances}">
            <Label Content="{Binding DeviceTypeName}"/>
        </HierarchicalDataTemplate>
    
        <DataTemplate DataType="{x:Type pc:MainWindow+DeviceInstance}">
            <Label Content="{Binding DeviceInstanceName}"/>
        </DataTemplate>
    
    </Window.Resources>
    

    About namespaces

    Namespace is what you need to know to use classes. If your classes have namespace:

    namespace MyCompany.MyProject.MyComponent
    {
       public class SystemInventory
       {
          ....
       }
    }
    

    This namespace should be added to XAML with alias to use:

    <Window x:Class="MyCompany.MyProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
        xmlns:myAlias="clr-namespace:MyCompany.MyProject.MyComponent" 
    
        Title="Window1" Height="300" Width="350">
        <Window.Resources>
        ...
    

    Now you can use this classes in XAML like:

    <DataTemplate DataType="{x:Type myAlias:DeviceInstance}">
       <Label Content="{Binding DeviceInstanceName}"/>
    </DataTemplate>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom-made view that extends the View class. I would like 2
I have a custom class that I use to build table strings. I would
I have a custom class that has as an instance variable of a coordinate:
I have a custom class that uses boost mutexes and locks like this (only
I have created a custom collection class that I am trying to bind to
I have a custom class that looks like: function myClass(){ var thing; var thingtype;
I have a custom class that acts as an int called Integer, I would
Basically I have a custom List class that contains different fruits. Assume that each
I have an actionscript file that defines a class that I would like to
I have a static class that I would like to raise an event as

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.