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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:30:31+00:00 2026-06-02T00:30:31+00:00

I am new in WPF and I would like to have a quick advice

  • 0

I am new in WPF and I would like to have a quick advice on how to bind this object to a wpf control and I dont know which control should I use:

public class Parent
{
  public string Name{get; set;}
  public List<Child> Childs {get; set;}
}

public class Child
{
  public string Name{get; set;}
  public int Age {get; set;}
}

public class ParentFactory
{
   public List<Parent> Parents {get; set;}

   public ParentFactory()
   {
      Child child1 = new Child() {Name="Peter", Age=10;};
      Child child2 = new Child() {Name="Mary", Age=9;};
      Child child3 = new Child() {Name="Becky", Age=12;};

      Parent parent1 = new Parent(){Name="Adam", Childs = new List<Child<>(){child1, child2}};
      Parent parent2 = new Parent(){Name="Kevin", Childs = new List<Child<>(){child3}};

      Parents = new List<Parent>(){parent1, parent2};
   }
}

After creating this instance:

ParentFactory parentFactory = new ParentFactory();

I would like to bind the parentFactory.Parents() to a control in WPF. I would expect to see something like this:


Adam

— Peter, 10

— Mary, 9

Kevin

— Becky, 12


They are all displayed on textboxes and I can change them.

Thanks in advance.

  • 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-06-02T00:30:32+00:00Added an answer on June 2, 2026 at 12:30 am

    Use a TreeView with a HierarchicalDataTemplate.

    Note however that without implement INotifyPropertyChanged on your model, your bindings won’t update on any property changes. Also, without replacing your lists with ObservableCollections, your view won’t update as you add more items to the list.

    Something like this should work, first define your templates:

    <Window.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Parent}" ItemsSource="{Binding Childs}">
            <TextBlock Text="{Binding Name}"/>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type local:Child}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
                <TextBlock>, </TextBlock>
                <TextBlock Text="{Binding Age}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    

    Then you can use them with a TreeView like this (assuming Parents is a property in the DataContext of the TreeView):

    <TreeView ItemsSource="{Binding Parents}"/>
    

    If you don’t want a TreeView, you can easily do something list this with a ListView, change you DataTemplates to this:

        <DataTemplate DataType="{x:Type local:Parent}">
            <StackPanel>
                <TextBlock Text="{Binding Name}"/>
                <ListView ItemsSource="{Binding Childs}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:Child}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
                <TextBlock>, </TextBlock>
                <TextBlock Text="{Binding Age}"/>
            </StackPanel>
        </DataTemplate>
    

    And then you can bind it like this:

    <ListView ItemsSource="{Binding Parents}"/>
    

    Note: You’ll probably want to fiddle around with the styles a little because out-of-the-box, this looks a bit crap. You’ll probably want to, at least, indent the child ListView (the one defined in the Parent DataTemplate) and get rid of it’s border.

    Also Note: the StackPanel to layout multiple TextBlocks for the name and age isn’t ideal either, but it’s quick and dirty. You might want to handle that differently. You could use a (multi) converter to format it, use StringFormat or add another property to your model just for display, or even just override ToString on the child class.

    Another Edit

    A quick (and ugly) example of using the DataGrid:

    <DataGrid ItemsSource="{Binding Parents}" AutoGenerateColumns="False">
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <DataGrid ItemsSource="{Binding Childs}"/>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"/>
        </DataGrid.Columns>
    </DataGrid>
    

    This puts a data grid inside a data grid using the row details template. If you click a row, it’ll display the children as row details. If you want details always available, you can remove the RowDetailsTemplate and replace the DataGridTextColumn with a DataGridTemplateColumn and then define a template for you data.

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

Sidebar

Related Questions

I have a custom class that I would like to bind a WPF TreeView
I have the following XAML in a WPF application. I would like to bind
I am relatively new to WPF and would like to create a UserControl that
I am currently working on a WPF application where I would like to have
In particular I would like to know how to bind the 'SelectionChanged' event of
I would like to achieve the new tab functionality of web browsers in WPF.
I have a collection of objects that look like this: List<MyObject> objects = new
I'm new to WPF. I have like 15 grids on my Window and I
I would like to know how people are going about validating collections in WPF.
How flexible is DataBinding in WPF? I am new to WPF and would like

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.